Compare commits

..

1 Commits
lab3 ... master

Author SHA1 Message Date
tangttangtang 78168fe917 重复纯归约优化
10 minutes ago

@ -9,6 +9,7 @@ bool RunConstFold(Module& module);
bool RunConstProp(Module& module);
bool RunFunctionInlining(Module& module);
bool RunTailRecursionElim(Module& module);
bool RunInterproceduralConstProp(Module& module);
bool RunArithmeticSimplify(Module& module);
bool RunCSE(Module& module);
bool RunGVN(Module& module);
@ -21,6 +22,8 @@ bool RunLoopUnswitch(Module& module);
bool RunLoopStrengthReduction(Module& module);
bool RunLoopUnroll(Module& module);
bool RunLoopFission(Module& module);
bool RunLoopRepeatReduction(Module& module);
bool RunIfConversion(Module& module);
void RunIRPassPipeline(Module& module);
} // namespace ir

@ -116,8 +116,9 @@ class MachineInstr {
ModMul,
ModPow,
DigitExtractPow2,
BitTestMask,
And,
Or,
Or,
Xor,
Shl,
AShr,

@ -3,6 +3,7 @@ add_library(ir_passes STATIC
Mem2Reg.cpp
ConstFold.cpp
ConstProp.cpp
InterproceduralConstProp.cpp
TailRecursionElim.cpp
ArithmeticSimplify.cpp
Inline.cpp
@ -17,6 +18,8 @@ add_library(ir_passes STATIC
LoopStrengthReduction.cpp
LoopUnroll.cpp
LoopFission.cpp
LoopRepeatReduction.cpp
IfConversion.cpp
)
target_link_libraries(ir_passes PUBLIC

@ -0,0 +1,239 @@
#include "ir/PassManager.h"
#include "ir/IR.h"
#include "PassUtils.h"
#include <cstddef>
#include <vector>
namespace ir {
namespace {
Instruction* GetTerminator(BasicBlock* block) {
if (block == nullptr || block->GetInstructions().empty()) {
return nullptr;
}
auto* inst = block->GetInstructions().back().get();
return inst != nullptr && inst->IsTerminator() ? inst : nullptr;
}
std::size_t GetTerminatorIndex(BasicBlock* block) {
const auto& instructions = block->GetInstructions();
return instructions.empty() ? 0 : instructions.size() - 1;
}
ConstantInt* ConstInt(int value) {
return new ConstantInt(Type::GetInt32Type(), value);
}
PhiInst* GetSinglePhi(BasicBlock* block) {
if (block == nullptr) {
return nullptr;
}
PhiInst* phi = nullptr;
for (const auto& inst_ptr : block->GetInstructions()) {
auto* current = dyncast<PhiInst>(inst_ptr.get());
if (current == nullptr) {
break;
}
if (phi != nullptr) {
return nullptr;
}
phi = current;
}
return phi;
}
bool HasOnlyOneNonTerminator(BasicBlock* block, Instruction** out) {
if (block == nullptr) {
return false;
}
Instruction* candidate = nullptr;
for (const auto& inst_ptr : block->GetInstructions()) {
auto* inst = inst_ptr.get();
if (inst == nullptr || inst->IsTerminator()) {
continue;
}
if (candidate != nullptr) {
return false;
}
candidate = inst;
}
if (out != nullptr) {
*out = candidate;
}
return candidate != nullptr;
}
int IncomingIndexFor(PhiInst* phi, BasicBlock* block) {
if (phi == nullptr || block == nullptr) {
return -1;
}
for (int i = 0; i < phi->GetNumIncomings(); ++i) {
if (phi->GetIncomingBlock(i) == block) {
return i;
}
}
return -1;
}
bool IsUsedOnlyBy(Value* value, User* expected_user) {
if (value == nullptr || expected_user == nullptr) {
return false;
}
for (const auto& use : value->GetUses()) {
if (use.GetUser() != expected_user) {
return false;
}
}
return true;
}
struct ConditionalAccumulation {
Value* base = nullptr;
Value* delta = nullptr;
Opcode opcode = Opcode::Add;
};
bool MatchConditionalAccumulation(PhiInst* phi, BasicBlock* pred,
BasicBlock* update_block,
BinaryInst* update,
ConditionalAccumulation* match) {
if (phi == nullptr || pred == nullptr || update_block == nullptr ||
update == nullptr || match == nullptr || phi->GetNumIncomings() != 2 ||
!phi->GetType()->IsInt32() || !update->GetType()->IsInt32()) {
return false;
}
const int pred_index = IncomingIndexFor(phi, pred);
const int update_index = IncomingIndexFor(phi, update_block);
if (pred_index < 0 || update_index < 0) {
return false;
}
auto* base = phi->GetIncomingValue(pred_index);
if (phi->GetIncomingValue(update_index) != update || base == nullptr ||
!base->GetType()->IsInt32() || !IsUsedOnlyBy(update, phi)) {
return false;
}
auto* lhs = update->GetLhs();
auto* rhs = update->GetRhs();
if (update->GetOpcode() == Opcode::Add) {
if (lhs == base && rhs != nullptr && rhs->GetType()->IsInt32()) {
*match = {base, rhs, Opcode::Add};
return true;
}
if (rhs == base && lhs != nullptr && lhs->GetType()->IsInt32()) {
*match = {base, lhs, Opcode::Add};
return true;
}
return false;
}
if (update->GetOpcode() == Opcode::Sub && lhs == base && rhs != nullptr &&
rhs->GetType()->IsInt32()) {
*match = {base, rhs, Opcode::Sub};
return true;
}
return false;
}
bool TryConvertConditionalAccumulation(Function& function, BasicBlock* pred) {
auto* branch = dyncast<CondBrInst>(GetTerminator(pred));
if (branch == nullptr || branch->GetCondition() == nullptr ||
!branch->GetCondition()->GetType()->IsInt1()) {
return false;
}
auto* update_block = branch->GetThenBlock();
auto* join = branch->GetElseBlock();
if (update_block == nullptr || join == nullptr || update_block == join ||
update_block->GetPredecessors().size() != 1 ||
update_block->GetPredecessors().front() != pred ||
update_block->GetSuccessors().size() != 1 ||
update_block->GetSuccessors().front() != join) {
return false;
}
auto* update_term = dyncast<UncondBrInst>(GetTerminator(update_block));
if (update_term == nullptr || update_term->GetDest() != join) {
return false;
}
Instruction* only_inst = nullptr;
if (!HasOnlyOneNonTerminator(update_block, &only_inst)) {
return false;
}
auto* update = dyncast<BinaryInst>(only_inst);
if (update == nullptr ||
(update->GetOpcode() != Opcode::Add && update->GetOpcode() != Opcode::Sub)) {
return false;
}
auto* phi = GetSinglePhi(join);
ConditionalAccumulation accum;
if (!MatchConditionalAccumulation(phi, pred, update_block, update, &accum)) {
return false;
}
const std::size_t insert_pos = GetTerminatorIndex(pred);
auto* enabled = pred->Insert<ZextInst>(insert_pos, branch->GetCondition(),
Type::GetInt32Type(), nullptr,
"%ifconv.zext");
auto* mask = pred->Insert<BinaryInst>(insert_pos + 1, Opcode::Sub,
Type::GetInt32Type(), ConstInt(0),
enabled, nullptr, "%ifconv.mask");
auto* masked_delta = pred->Insert<BinaryInst>(
insert_pos + 2, Opcode::And, Type::GetInt32Type(), accum.delta, mask,
nullptr, "%ifconv.delta");
auto* replacement = pred->Insert<BinaryInst>(
insert_pos + 3, accum.opcode, Type::GetInt32Type(), accum.base,
masked_delta, nullptr, "%ifconv.acc");
phi->ReplaceAllUsesWith(replacement);
join->EraseInstruction(phi);
passutils::ReplaceTerminatorWithBr(pred, join);
pred->RemoveSuccessor(update_block);
update_block->RemovePredecessor(pred);
passutils::RemoveUnreachableBlocks(function);
return true;
}
bool RunIfConversionOnFunction(Function& function) {
if (function.IsExternal()) {
return false;
}
bool changed = false;
bool local_changed = true;
while (local_changed) {
local_changed = false;
auto blocks = passutils::CollectReachableBlocks(function);
for (auto* block : blocks) {
if (TryConvertConditionalAccumulation(function, block)) {
local_changed = true;
changed = true;
break;
}
}
}
return changed;
}
} // namespace
bool RunIfConversion(Module& module) {
bool changed = false;
for (const auto& function : module.GetFunctions()) {
if (function != nullptr) {
changed |= RunIfConversionOnFunction(*function);
}
}
return changed;
}
} // namespace ir

@ -0,0 +1,145 @@
#include "ir/PassManager.h"
#include "ir/IR.h"
#include "PassUtils.h"
#include <vector>
namespace ir {
namespace {
bool IsScalarConstant(Value* value) {
return dyncast<ConstantInt>(value) != nullptr ||
dyncast<ConstantI1>(value) != nullptr ||
dyncast<ConstantFloat>(value) != nullptr;
}
bool IsScalarType(const std::shared_ptr<Type>& type) {
return type && (type->IsInt32() || type->IsInt1() || type->IsFloat());
}
bool IsReadonlyScalarGlobal(GlobalValue* global) {
if (global == nullptr || !IsScalarType(global->GetObjectType()) ||
!IsScalarConstant(global->GetInitializer())) {
return false;
}
for (const auto& use : global->GetUses()) {
auto* user = dyncast<Instruction>(use.GetUser());
if (auto* load = dyncast<LoadInst>(user)) {
if (load->GetPtr() == global) {
continue;
}
}
return false;
}
return true;
}
bool PropagateReadonlyScalarGlobals(Module& module) {
bool changed = false;
std::vector<LoadInst*> dead_loads;
for (const auto& global_ptr : module.GetGlobalValues()) {
auto* global = global_ptr.get();
if (!IsReadonlyScalarGlobal(global)) {
continue;
}
const auto uses = global->GetUses();
for (const auto& use : uses) {
auto* load = dyncast<LoadInst>(use.GetUser());
if (load == nullptr || load->GetPtr() != global) {
continue;
}
load->ReplaceAllUsesWith(global->GetInitializer());
dead_loads.push_back(load);
changed = true;
}
}
for (auto* load : dead_loads) {
if (load != nullptr && load->GetParent() != nullptr && load->GetUses().empty()) {
load->GetParent()->EraseInstruction(load);
}
}
return changed;
}
std::vector<CallInst*> CollectDirectCalls(Function& function, bool* all_uses_are_calls) {
std::vector<CallInst*> calls;
*all_uses_are_calls = true;
for (const auto& use : function.GetUses()) {
if (use.GetOperandIndex() != 0) {
*all_uses_are_calls = false;
return {};
}
auto* call = dyncast<CallInst>(use.GetUser());
if (call == nullptr || call->GetCallee() != &function) {
*all_uses_are_calls = false;
return {};
}
calls.push_back(call);
}
return calls;
}
bool PropagateConstantArguments(Function& function) {
if (function.IsExternal() || function.GetName() == "main" ||
function.GetArguments().empty()) {
return false;
}
bool all_uses_are_calls = false;
auto calls = CollectDirectCalls(function, &all_uses_are_calls);
if (!all_uses_are_calls || calls.empty()) {
return false;
}
bool changed = false;
for (std::size_t index = 0; index < function.GetArguments().size(); ++index) {
auto* argument = function.GetArgument(index);
if (argument == nullptr || !IsScalarType(argument->GetType()) ||
argument->GetUses().empty()) {
continue;
}
Value* constant = nullptr;
bool same_constant = true;
for (auto* call : calls) {
const auto args = call->GetArguments();
if (index >= args.size() || !IsScalarConstant(args[index])) {
same_constant = false;
break;
}
if (constant == nullptr) {
constant = args[index];
} else if (!passutils::AreEquivalentValues(constant, args[index])) {
same_constant = false;
break;
}
}
if (!same_constant || constant == nullptr) {
continue;
}
argument->ReplaceAllUsesWith(constant);
changed = true;
}
return changed;
}
} // namespace
bool RunInterproceduralConstProp(Module& module) {
bool changed = false;
changed |= PropagateReadonlyScalarGlobals(module);
for (const auto& function : module.GetFunctions()) {
if (function != nullptr) {
changed |= PropagateConstantArguments(*function);
}
}
return changed;
}
} // namespace ir

@ -0,0 +1,264 @@
#include "ir/PassManager.h"
#include "ir/Analysis.h"
#include "ir/IR.h"
#include "ir/passes/LoopPassUtils.h"
#include <queue>
#include <unordered_set>
#include <vector>
namespace ir {
namespace {
bool IsConstInt(Value* value, int expected) {
auto* constant = dyncast<ConstantInt>(value);
return constant != nullptr && constant->GetValue() == expected;
}
bool IsAddOneOf(Value* value, Value* base) {
auto* add = dyncast<BinaryInst>(value);
if (!add || add->GetOpcode() != Opcode::Add) {
return false;
}
return (add->GetLhs() == base && IsConstInt(add->GetRhs(), 1)) ||
(add->GetRhs() == base && IsConstInt(add->GetLhs(), 1));
}
bool HasForbiddenSideEffects(const Loop& loop) {
for (auto* block : loop.block_list) {
for (const auto& inst_ptr : block->GetInstructions()) {
auto* inst = inst_ptr.get();
switch (inst->GetOpcode()) {
case Opcode::Store:
case Opcode::Memset:
case Opcode::Call:
return true;
default:
break;
}
}
}
return false;
}
bool HasUseOutsideLoop(Value* value, const Loop& loop) {
for (const auto& use : value->GetUses()) {
auto* inst = dyncast<Instruction>(use.GetUser());
if (!inst || !loop.Contains(inst->GetParent())) {
return true;
}
}
return false;
}
bool InductionOnlyControlsRepeatCount(PhiInst* induction, BinaryInst* compare,
BinaryInst* next, const Loop& loop) {
for (const auto& use : induction->GetUses()) {
auto* inst = dyncast<Instruction>(use.GetUser());
if (!inst) {
return false;
}
if (inst == compare || inst == next) {
continue;
}
if (loop.Contains(inst->GetParent())) {
return false;
}
}
return true;
}
bool IsAdditiveAccumulator(PhiInst* accumulator, BasicBlock* preheader,
BasicBlock* latch, const Loop& loop) {
if (!accumulator || !accumulator->IsInt32()) {
return false;
}
const int preheader_index = looputils::GetPhiIncomingIndex(accumulator, preheader);
const int latch_index = looputils::GetPhiIncomingIndex(accumulator, latch);
if (preheader_index < 0 || latch_index < 0) {
return false;
}
if (!IsConstInt(accumulator->GetIncomingValue(preheader_index), 0)) {
return false;
}
auto* latch_value = accumulator->GetIncomingValue(latch_index);
if (latch_value == accumulator) {
return false;
}
std::unordered_set<Value*> derived;
std::vector<BinaryInst*> additive_steps;
std::queue<Value*> worklist;
derived.insert(accumulator);
worklist.push(accumulator);
auto remember = [&](Value* value) {
if (derived.insert(value).second) {
worklist.push(value);
}
};
while (!worklist.empty()) {
auto* value = worklist.front();
worklist.pop();
for (const auto& use : value->GetUses()) {
auto* inst = dyncast<Instruction>(use.GetUser());
if (!inst || !loop.Contains(inst->GetParent())) {
continue;
}
if (auto* phi = dyncast<PhiInst>(inst)) {
remember(phi);
continue;
}
auto* binary = dyncast<BinaryInst>(inst);
if (!binary || binary->GetOpcode() != Opcode::Add) {
return false;
}
additive_steps.push_back(binary);
remember(binary);
}
}
if (derived.find(latch_value) == derived.end()) {
return false;
}
for (auto* add : additive_steps) {
const bool lhs_derived = derived.find(add->GetLhs()) != derived.end();
const bool rhs_derived = derived.find(add->GetRhs()) != derived.end();
if (lhs_derived == rhs_derived) {
return false;
}
}
return true;
}
std::vector<PhiInst*> GetHeaderPhis(BasicBlock* header) {
std::vector<PhiInst*> phis;
if (!header) {
return phis;
}
for (const auto& inst_ptr : header->GetInstructions()) {
auto* phi = dyncast<PhiInst>(inst_ptr.get());
if (!phi) {
break;
}
phis.push_back(phi);
}
return phis;
}
bool TryReduceRepeatLoop(Function& function, Loop& loop) {
if (!loop.header || !loop.preheader || loop.latches.size() != 1 ||
loop.exit_blocks.size() != 1 || HasForbiddenSideEffects(loop)) {
return false;
}
auto* latch = loop.latches.front();
auto* exit = loop.exit_blocks.front();
auto* branch =
dyncast<CondBrInst>(looputils::GetTerminator(loop.header));
if (!branch) {
return false;
}
auto* compare = dyncast<BinaryInst>(branch->GetCondition());
if (!compare || compare->GetOpcode() != Opcode::ICmpLT) {
return false;
}
if (!loop.Contains(branch->GetThenBlock()) || branch->GetElseBlock() != exit) {
return false;
}
auto* induction = dyncast<PhiInst>(compare->GetLhs());
auto* bound = compare->GetRhs();
if (!induction || induction->GetParent() != loop.header ||
!looputils::IsLoopInvariantValue(loop, bound)) {
return false;
}
const int induction_preheader_index =
looputils::GetPhiIncomingIndex(induction, loop.preheader);
const int induction_latch_index = looputils::GetPhiIncomingIndex(induction, latch);
if (induction_preheader_index < 0 || induction_latch_index < 0 ||
!IsConstInt(induction->GetIncomingValue(induction_preheader_index), 0)) {
return false;
}
auto* induction_next =
dyncast<BinaryInst>(induction->GetIncomingValue(induction_latch_index));
if (!IsAddOneOf(induction_next, induction) ||
!InductionOnlyControlsRepeatCount(induction, compare, induction_next, loop)) {
return false;
}
std::vector<PhiInst*> accumulators;
for (auto* phi : GetHeaderPhis(loop.header)) {
if (phi == induction) {
continue;
}
if (!IsAdditiveAccumulator(phi, loop.preheader, latch, loop)) {
return false;
}
if (HasUseOutsideLoop(phi, loop)) {
accumulators.push_back(phi);
}
}
if (accumulators.empty()) {
return false;
}
// Force the counted loop to stop after one executed body: the first test still
// uses 0 < bound, so non-positive trip counts continue to execute zero times.
induction->SetOperand(static_cast<std::size_t>(2 * induction_latch_index), bound);
std::size_t insert_index = looputils::GetFirstNonPhiIndex(exit);
bool changed = true;
for (auto* accumulator : accumulators) {
auto* scaled = exit->Insert<BinaryInst>(
insert_index++, Opcode::Mul, Type::GetInt32Type(), accumulator, bound,
nullptr, looputils::NextSyntheticName(function, "repeat.reduce"));
const auto uses = accumulator->GetUses();
for (const auto& use : uses) {
auto* user = use.GetUser();
auto* user_inst = dyncast<Instruction>(user);
if (user_inst == scaled) {
continue;
}
if (!user_inst || !loop.Contains(user_inst->GetParent())) {
user->SetOperand(use.GetOperandIndex(), scaled);
}
}
}
return changed;
}
bool RunOnFunction(Function& function) {
DominatorTree dom_tree(function);
LoopInfo loop_info(function, dom_tree);
bool changed = false;
for (auto* loop : loop_info.GetLoopsInPostOrder()) {
changed |= TryReduceRepeatLoop(function, *loop);
}
return changed;
}
} // namespace
bool RunLoopRepeatReduction(Module& module) {
bool changed = false;
for (const auto& function : module.GetFunctions()) {
if (function && !function->IsExternal()) {
changed |= RunOnFunction(*function);
}
}
return changed;
}
} // namespace ir

@ -5,6 +5,7 @@
#include "LoopPassUtils.h"
#include <cstdlib>
#include <limits>
#include <unordered_map>
#include <vector>
@ -18,6 +19,12 @@ struct InductionVarInfo {
int stride = 0;
};
struct GepReductionCandidate {
GetElementPtrInst* gep = nullptr;
std::vector<Value*> init_indices;
int step_elements = 0;
};
Value* BuildMulValue(Function& function, BasicBlock* block, Value* lhs, Value* rhs,
const std::string& prefix) {
if (auto* lhs_const = dyncast<ConstantInt>(lhs)) {
@ -248,6 +255,160 @@ bool ReduceLoopMultiplications(Function& function, const Loop& loop,
return changed;
}
bool LoopHasCallsOrMemset(const Loop& loop) {
for (auto* block : loop.block_list) {
for (const auto& inst_ptr : block->GetInstructions()) {
auto* inst = inst_ptr.get();
if (dyncast<CallInst>(inst) || dyncast<MemsetInst>(inst)) {
return true;
}
}
}
return false;
}
bool DominatesBlock(const DominatorTree& dom_tree, Value* value,
BasicBlock* block) {
auto* inst = dyncast<Instruction>(value);
return inst == nullptr ||
(inst->GetParent() != nullptr && dom_tree.Dominates(inst->GetParent(), block));
}
bool BuildGepReductionCandidate(const Loop& loop, const InductionVarInfo& iv,
const DominatorTree& dom_tree,
BasicBlock* preheader,
GetElementPtrInst* gep,
GepReductionCandidate& candidate) {
if (gep == nullptr || preheader == nullptr ||
!looputils::IsLoopInvariantValue(loop, gep->GetPointer()) ||
!DominatesBlock(dom_tree, gep->GetPointer(), preheader)) {
return false;
}
auto current_type = gep->GetSourceType();
std::int64_t step_bytes = 0;
bool saw_iv = false;
std::vector<Value*> init_indices;
init_indices.reserve(gep->GetNumIndices());
for (std::size_t i = 0; i < gep->GetNumIndices(); ++i) {
auto* index = gep->GetIndex(i);
const std::int64_t stride =
current_type ? static_cast<std::int64_t>(current_type->GetSize()) : 0;
if (index == iv.phi) {
saw_iv = true;
step_bytes += stride * static_cast<std::int64_t>(iv.stride);
init_indices.push_back(iv.start);
} else {
if (!looputils::IsLoopInvariantValue(loop, index) ||
!DominatesBlock(dom_tree, index, preheader)) {
return false;
}
init_indices.push_back(index);
}
if (current_type && current_type->IsArray()) {
current_type = current_type->GetElementType();
}
}
if (!saw_iv || step_bytes == 0 || step_bytes % 4 != 0) {
return false;
}
const std::int64_t step_elements = step_bytes / 4;
if (step_elements < std::numeric_limits<int>::min() ||
step_elements > std::numeric_limits<int>::max()) {
return false;
}
candidate.gep = gep;
candidate.init_indices = std::move(init_indices);
candidate.step_elements = static_cast<int>(step_elements);
return true;
}
Value* CreateReducedPointerPhi(Function& function, const Loop& loop,
BasicBlock* preheader,
const GepReductionCandidate& candidate) {
auto* init = preheader->Insert<GetElementPtrInst>(
looputils::GetTerminatorIndex(preheader), candidate.gep->GetSourceType(),
candidate.gep->GetPointer(), candidate.init_indices, nullptr,
looputils::NextSyntheticName(function, "lsr.ptr.init."));
auto* ptr_phi = loop.header->Insert<PhiInst>(
looputils::GetFirstNonPhiIndex(loop.header), Type::GetPointerType(), nullptr,
looputils::NextSyntheticName(function, "lsr.ptr.phi."));
ptr_phi->AddIncoming(init, preheader);
auto* next = candidate.step_elements == 0
? static_cast<Value*>(ptr_phi)
: static_cast<Value*>(loop.latches.front()->Insert<GetElementPtrInst>(
looputils::GetTerminatorIndex(loop.latches.front()),
Type::GetInt32Type(), ptr_phi,
std::vector<Value*>{looputils::ConstInt(candidate.step_elements)},
nullptr, looputils::NextSyntheticName(function, "lsr.ptr.next.")));
ptr_phi->AddIncoming(next, loop.latches.front());
return ptr_phi;
}
bool ReduceLoopAddressing(Function& function, const Loop& loop,
const DominatorTree& dom_tree, BasicBlock* preheader) {
if (!preheader || !loop.IsInnermost() || loop.latches.size() != 1 ||
LoopHasCallsOrMemset(loop)) {
return false;
}
std::vector<InductionVarInfo> induction_vars;
for (const auto& inst_ptr : loop.header->GetInstructions()) {
auto* phi = dyncast<PhiInst>(inst_ptr.get());
if (!phi) {
break;
}
InductionVarInfo info;
if (MatchSimpleInductionVariable(loop, preheader, phi, info)) {
induction_vars.push_back(info);
}
}
if (induction_vars.empty()) {
return false;
}
bool changed = false;
std::vector<Instruction*> to_remove;
for (const auto& iv : induction_vars) {
std::vector<GepReductionCandidate> candidates;
for (auto* block : loop.block_list) {
for (const auto& inst_ptr : block->GetInstructions()) {
auto* gep = dyncast<GetElementPtrInst>(inst_ptr.get());
GepReductionCandidate candidate;
if (BuildGepReductionCandidate(loop, iv, dom_tree, preheader, gep,
candidate)) {
candidates.push_back(std::move(candidate));
}
}
}
for (const auto& candidate : candidates) {
if (candidate.gep == nullptr || candidate.gep->GetParent() == nullptr ||
candidate.gep->GetUses().empty()) {
continue;
}
auto* replacement = CreateReducedPointerPhi(function, loop, preheader, candidate);
candidate.gep->ReplaceAllUsesWith(replacement);
to_remove.push_back(candidate.gep);
changed = true;
}
}
for (auto* inst : to_remove) {
if (inst && inst->GetParent() && inst->GetUses().empty()) {
inst->GetParent()->EraseInstruction(inst);
}
}
return changed;
}
bool RunLoopStrengthReductionOnFunction(Function& function) {
if (function.IsExternal() || function.GetEntryBlock() == nullptr) {
return false;
@ -264,6 +425,7 @@ bool RunLoopStrengthReductionOnFunction(Function& function) {
auto* preheader = looputils::EnsurePreheader(function, *loop);
bool loop_changed = preheader != old_preheader;
loop_changed |= ReduceLoopMultiplications(function, *loop, preheader);
loop_changed |= ReduceLoopAddressing(function, *loop, dom_tree, preheader);
if (!loop_changed) {
continue;
}

@ -44,12 +44,14 @@ void RunIRPassPipeline(Module& module) {
if (run_cfg_inline) {
changed |= RunFunctionInlining(module);
}
changed |= RunInterproceduralConstProp(module);
changed |= RunArithmeticSimplify(module);
changed |= RunConstProp(module);
changed |= RunConstFold(module);
changed |= RunGVN(module);
changed |= RunLoadStoreElim(module);
changed |= RunCSE(module);
changed |= RunIfConversion(module);
changed |= RunDCE(module);
changed |= RunCFGSimplify(module);
changed |= RunLICM(module);
@ -62,12 +64,14 @@ void RunIRPassPipeline(Module& module) {
changed |= RunLoopStrengthReduction(module);
changed |= RunLoopFission(module);
changed |= RunLoopUnroll(module);
changed |= RunLoopRepeatReduction(module);
changed |= RunArithmeticSimplify(module);
changed |= RunConstProp(module);
changed |= RunConstFold(module);
changed |= RunGVN(module);
changed |= RunLoadStoreElim(module);
changed |= RunCSE(module);
changed |= RunIfConversion(module);
changed |= RunDCE(module);
changed |= RunCFGSimplify(module);
if (!changed) {

@ -1863,6 +1863,17 @@ void EmitFunction(const MachineFunction& function, std::ostream& os) {
FinalizeDef(function, vreg, def, os);
break;
}
case MachineInstr::Opcode::BitTestMask: {
const int vreg = inst.GetOperands()[0].GetVReg();
const auto def = PrepareGprDef(function, vreg, 9);
const auto value =
MaterializeGprUse(function, inst.GetOperands()[1], ValueType::I32, 10, os);
os << " tst " << value << ", #1\n";
os << " csetm " << def.reg_name << ", "
<< GetIntCondMnemonic(inst.GetCondCode()) << "\n";
FinalizeDef(function, vreg, def, os);
break;
}
case MachineInstr::Opcode::FAdd:
case MachineInstr::Opcode::FSub:
case MachineInstr::Opcode::FMul:

@ -44,6 +44,7 @@ std::vector<int> MachineInstr::GetDefs() const {
case Opcode::ModMul:
case Opcode::ModPow:
case Opcode::DigitExtractPow2:
case Opcode::BitTestMask:
case Opcode::And:
case Opcode::Or:
case Opcode::Xor:
@ -126,6 +127,11 @@ std::vector<int> MachineInstr::GetUses() const {
}
push_addr_uses();
break;
case Opcode::BitTestMask:
if (operands_.size() >= 2) {
push_vreg(operands_[1]);
}
break;
case Opcode::Add:
case Opcode::Sub:
case Opcode::Mul:

@ -204,6 +204,11 @@ bool RewriteUses(MachineInstr& inst, const AliasMap& aliases) {
changed |= RewriteOperand(operands[0], aliases);
}
break;
case MachineInstr::Opcode::BitTestMask:
if (operands.size() >= 2) {
changed |= RewriteOperand(operands[1], aliases);
}
break;
case MachineInstr::Opcode::Add:
case MachineInstr::Opcode::Sub:
case MachineInstr::Opcode::Mul:
@ -693,6 +698,8 @@ MemoryMap SimulateBlockMemory(const MachineModule& module, const MachineBasicBlo
return state;
}
bool CombineBitTestMasks(std::vector<MachineInstr>& instructions);
bool RunPeepholeOnBlock(const MachineModule& module, const MachineFunction& function,
MachineBasicBlock& block, const MemoryMap& in_state) {
bool changed = false;
@ -757,6 +764,7 @@ bool RunPeepholeOnBlock(const MachineModule& module, const MachineFunction& func
if (compacted.size() != block.GetInstructions().size()) {
changed = true;
}
changed |= CombineBitTestMasks(compacted);
if (changed) {
block.GetInstructions() = std::move(compacted);
}
@ -777,6 +785,7 @@ bool IsSideEffectFree(const MachineInstr& inst) {
case MachineInstr::Opcode::ModMul:
case MachineInstr::Opcode::ModPow:
case MachineInstr::Opcode::DigitExtractPow2:
case MachineInstr::Opcode::BitTestMask:
case MachineInstr::Opcode::And:
case MachineInstr::Opcode::Or:
case MachineInstr::Opcode::Xor:
@ -808,6 +817,106 @@ bool IsSideEffectFree(const MachineInstr& inst) {
return false;
}
std::unordered_map<int, int> CountBlockUses(const std::vector<MachineInstr>& instructions) {
std::unordered_map<int, int> counts;
for (const auto& inst : instructions) {
for (int use : inst.GetUses()) {
++counts[use];
}
}
return counts;
}
bool GetDefVReg(const MachineInstr& inst, int* out) {
const auto defs = inst.GetDefs();
if (defs.size() != 1) {
return false;
}
if (out != nullptr) {
*out = defs.front();
}
return true;
}
bool IsVRegUse(const MachineOperand& operand, int vreg) {
return operand.GetKind() == OperandKind::VReg && operand.GetVReg() == vreg;
}
bool IsZeroCompareOperand(const MachineOperand& operand) {
return operand.GetKind() == OperandKind::Imm && operand.GetImm() == 0;
}
bool TryGetAndOneValue(const MachineInstr& inst, MachineOperand* value) {
if (inst.GetOpcode() != MachineInstr::Opcode::And ||
inst.GetOperands().size() < 3) {
return false;
}
const auto& lhs = inst.GetOperands()[1];
const auto& rhs = inst.GetOperands()[2];
if (IsImm(rhs, 1) && lhs.GetKind() == OperandKind::VReg) {
if (value != nullptr) {
*value = lhs;
}
return true;
}
if (IsImm(lhs, 1) && rhs.GetKind() == OperandKind::VReg) {
if (value != nullptr) {
*value = rhs;
}
return true;
}
return false;
}
bool CombineBitTestMasks(std::vector<MachineInstr>& instructions) {
bool changed = false;
auto use_counts = CountBlockUses(instructions);
for (std::size_t i = 0; i + 3 < instructions.size();) {
auto& and_inst = instructions[i];
auto& cmp_inst = instructions[i + 1];
auto& zext_inst = instructions[i + 2];
auto& sub_inst = instructions[i + 3];
MachineOperand tested_value;
int and_def = -1;
int cmp_def = -1;
int zext_def = -1;
if (!TryGetAndOneValue(and_inst, &tested_value) ||
!GetDefVReg(and_inst, &and_def) ||
cmp_inst.GetOpcode() != MachineInstr::Opcode::ICmp ||
!GetDefVReg(cmp_inst, &cmp_def) ||
zext_inst.GetOpcode() != MachineInstr::Opcode::ZExt ||
!GetDefVReg(zext_inst, &zext_def) ||
sub_inst.GetOpcode() != MachineInstr::Opcode::Sub ||
cmp_inst.GetOperands().size() < 3 ||
zext_inst.GetOperands().size() < 2 ||
sub_inst.GetOperands().size() < 3 ||
use_counts[and_def] != 1 || use_counts[cmp_def] != 1 ||
use_counts[zext_def] != 1 ||
!IsVRegUse(cmp_inst.GetOperands()[1], and_def) ||
!IsZeroCompareOperand(cmp_inst.GetOperands()[2]) ||
!IsVRegUse(zext_inst.GetOperands()[1], cmp_def) ||
!IsImm(sub_inst.GetOperands()[1], 0) ||
!IsVRegUse(sub_inst.GetOperands()[2], zext_def) ||
(cmp_inst.GetCondCode() != CondCode::EQ &&
cmp_inst.GetCondCode() != CondCode::NE)) {
++i;
continue;
}
MachineInstr mask(MachineInstr::Opcode::BitTestMask,
{sub_inst.GetOperands()[0], tested_value});
mask.SetCondCode(cmp_inst.GetCondCode());
instructions[i] = std::move(mask);
instructions.erase(instructions.begin() + static_cast<std::ptrdiff_t>(i + 1),
instructions.begin() + static_cast<std::ptrdiff_t>(i + 4));
changed = true;
use_counts = CountBlockUses(instructions);
}
return changed;
}
bool RunDeadInstrElimination(MachineFunction& function) {
bool changed = false;

@ -119,6 +119,11 @@ bool RewriteUses(MachineInstr& inst, const std::unordered_map<int, int>& rename_
changed |= RewriteMappedOperand(operands[0], rename_map);
}
break;
case MachineInstr::Opcode::BitTestMask:
if (operands.size() >= 2) {
changed |= RewriteMappedOperand(operands[1], rename_map);
}
break;
case MachineInstr::Opcode::Add:
case MachineInstr::Opcode::Sub:
case MachineInstr::Opcode::Mul:

@ -0,0 +1,9 @@
int main(){
const int a[4][2] = {{1, 2}, {3, 4}, {}, 7};
const int N = 3;
int b[4][2] = {};
int c[4][2] = {1, 2, 3, 4, 5, 6, 7, 8};
int d[N + 1][2] = {1, 2, {3}, {5}, a[3][0], 8};
int e[4][2][1] = {{d[2][1], {c[2][1]}}, {3, 4}, {5, 6}, {7, 8}};
return e[3][1][0] + e[0][0][0] + e[0][1][0] + d[3][0];
}

@ -0,0 +1,11 @@
int a;
int func(int p){
p = p - 1;
return p;
}
int main(){
int b;
a = 10;
b = func(a);
return b;
}

@ -0,0 +1,7 @@
//test add
int main(){
int a, b;
a = 10;
b = -1;
return a + b;
}

@ -0,0 +1,7 @@
//test sub
const int a = 10;
int main(){
int b;
b = 2;
return b - a;
}

@ -0,0 +1,69 @@
const int V = 4;
const int space = 32;
const int LF = 10;
void printSolution(int color[]) {
int i = 0;
while (i < V) {
putint(color[i]);
putch(space);
i = i + 1;
}
putch(LF);
}
void printMessage() {
putch(78);putch(111);putch(116);
putch(space);
putch(101);putch(120);putch(105);putch(115);putch(116);
}
int isSafe(int graph[][V], int color[]) {
int i = 0;
while (i < V) {
int j = i + 1;
while (j < V) {
if (graph[i][j] && color[j] == color[i])
return 0;
j = j + 1;
}
i = i + 1;
}
return 1;
}
int graphColoring(int graph[][V], int m, int i, int color[]) {
if (i == V) {
if (isSafe(graph, color)) {
printSolution(color);
return 1;
}
return 0;
}
int j = 1;
while (j <= m) {
color[i] = j;
if (graphColoring(graph, m, i + 1, color))
return 1;
color[i] = 0;
j = j + 1;
}
return 0;
}
int main() {
int graph[V][V] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}
}, m = 3;
int color[V], i = 0;
while (i < V) {
color[i] = 0;
i = i + 1;
}
if (!graphColoring(graph, m, 0, color))
printMessage();
return 0;
}

@ -0,0 +1,10 @@
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
4 3
9 5 1
10 6 2
11 7 3
12 8 4

@ -0,0 +1,60 @@
const int MAX_SIZE = 100;
int a[MAX_SIZE][MAX_SIZE], b[MAX_SIZE][MAX_SIZE];
int res[MAX_SIZE][MAX_SIZE];
int n1, m1, n2, m2;
void matrix_multiply() {
int i = 0;
while (i < m1) {
int j = 0;
while (j < n2) {
int k = 0;
while (k < n1) {
res[i][j] = res[i][j] + a[i][k] * b[k][j];
k = k + 1;
}
j = j + 1;
}
i = i + 1;
}
}
int main()
{
int i, j;
m1 = getint();
n1 = getint();
i = 0;
while (i < m1) {
j = 0;
while (j < n1) {
a[i][j] = getint();
j = j + 1;
}
i = i + 1;
}
m2 = getint();
n2 = getint();
i = 0;
while (i < m2) {
j = 0;
while (j < n2) {
b[i][j] = getint();
j = j + 1;
}
i = i + 1;
}
matrix_multiply();
i = 0;
while (i < m1) {
j = 0;
while (j < n2) {
putint(res[i][j]);
putch(32);
j = j + 1;
}
putch(10);
i = i + 1;
}
return 0;
}

@ -0,0 +1 @@
int main() { /* scope test */ putch(97); putch(10); int a = 1, putch = 0; { a = a + 2; int b = a + 3; b = b + 4; putch = putch + a + b; { b = b + 5; int main = b + 6; a = a + main; putch = putch + a + b + main; { b = b + a; int a = main + 7; a = a + 8; putch = putch + a + b + main; { b = b + a; int b = main + 9; a = a + 10; const int a = 11; b = b + 12; putch = putch + a + b + main; { main = main + b; int main = b + 13; main = main + a; putch = putch + a + b + main; } putch = putch - main; } putch = putch - b; } putch = putch - a; } } return putch % 77; }

@ -0,0 +1,15 @@
//test break
int main(){
int i;
i = 0;
int sum;
sum = 0;
while(i < 100){
if(i == 50){
break;
}
sum = sum + i;
i = i + 1;
}
return sum;
}

@ -0,0 +1,9 @@
//test the priority of add and mul
int main(){
int a, b, c, d;
a = 10;
b = 4;
c = 2;
d = 2;
return (c + a) * (b - d);
}

@ -0,0 +1,13 @@
10
0x1.999999999999ap-4 0x1.999999999999ap-3 0x1.3333333333333p-2 0x1.999999999999ap-2 0x1.0000000000000p-1
0x1.3333333333333p-1 0x1.6666666666666p-1 0x1.999999999999ap-1 0x1.ccccccccccccdp-1 0x1.0000000000000p+0
0x1.199999999999ap+0
0x1.199999999999ap+1
0x1.a666666666666p+1
0x1.199999999999ap+2
0x1.6000000000000p+2
0x1.a666666666666p+2
0x1.ecccccccccccdp+2
0x1.199999999999ap+3
0x1.3cccccccccccdp+3
0x1.4333333333333p+3

@ -0,0 +1,98 @@
// float global constants
const float RADIUS = 5.5, PI = 03.141592653589793, EPS = 1e-6;
// hexadecimal float constant
const float PI_HEX = 0x1.921fb6p+1, HEX2 = 0x.AP-3;
// float constant evaluation
const float FACT = -.33E+5, EVAL1 = PI * RADIUS * RADIUS, EVAL2 = 2 * PI_HEX * RADIUS, EVAL3 = PI * 2 * RADIUS;
// float constant implicit conversion
const float CONV1 = 233, CONV2 = 0xfff;
const int MAX = 1e9, TWO = 2.9, THREE = 3.2, FIVE = TWO + THREE;
// float -> float function
float float_abs(float x) {
if (x < 0) return -x;
return x;
}
// int -> float function & float/int expression
float circle_area(int radius) {
return (PI * radius * radius + (radius * radius) * PI) / 2;
}
// float -> float -> int function & float/int expression
int float_eq(float a, float b) {
if (float_abs(a - b) < EPS) {
return 1 * 2. / 2;
} else {
return 0;
}
}
void error() {
putch(101);
putch(114);
putch(114);
putch(111);
putch(114);
putch(10);
}
void ok() {
putch(111);
putch(107);
putch(10);
}
void assert(int cond) {
if (!cond) {
error();
} else {
ok();
}
}
void assert_not(int cond) {
if (cond) {
error();
} else {
ok();
}
}
int main() {
assert_not(float_eq(HEX2, FACT));
assert_not(float_eq(EVAL1, EVAL2));
assert(float_eq(EVAL2, EVAL3));
assert(float_eq(circle_area(RADIUS) /* f->i implicit conversion */,
circle_area(FIVE)));
assert_not(float_eq(CONV1, CONV2) /* i->f implicit conversion */);
// float conditional expressions
if (1.5) ok();
if (!!3.3) ok();
if (.0 && 3) error();
if (0 || 0.3) ok();
// float array & I/O functions
int i = 1, p = 0;
float arr[10] = {1., 2};
int len = getfarray(arr);
while (i < MAX) {
float input = getfloat();
float area = PI * input * input, area_trunc = circle_area(input);
arr[p] = arr[p] + input;
putfloat(area);
putch(32);
putint(area_trunc); // f->i implicit conversion
putch(10);
i = i * - -1e1;
p = p + 1;
}
putfarray(len, arr);
return 0;
}

@ -0,0 +1,5 @@
int main() {
int a = 1;
int b = 2;
return a + b;
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,89 @@
const int N = 1024;
void mm(int n, int A[][N], int B[][N], int C[][N]){
int i, j, k;
i = 0; j = 0;
while (i < n){
j = 0;
while (j < n){
C[i][j] = 0;
j = j + 1;
}
i = i + 1;
}
i = 0; j = 0; k = 0;
while (k < n){
i = 0;
while (i < n){
if (A[i][k] == 0){
i = i + 1;
continue;
}
j = 0;
while (j < n){
C[i][j] = C[i][j] + A[i][k] * B[k][j];
j = j + 1;
}
i = i + 1;
}
k = k + 1;
}
}
int A[N][N];
int B[N][N];
int C[N][N];
int main(){
int n = getint();
int i, j;
i = 0;
j = 0;
while (i < n){
j = 0;
while (j < n){
A[i][j] = getint();
j = j + 1;
}
i = i + 1;
}
i = 0;
j = 0;
while (i < n){
j = 0;
while (j < n){
B[i][j] = getint();
j = j + 1;
}
i = i + 1;
}
starttime();
i = 0;
while (i < 5){
mm(n, A, B, C);
mm(n, A, C, B);
i = i + 1;
}
int ans = 0;
i = 0;
while (i < n){
j = 0;
while (j < n){
ans = ans + B[i][j];
j = j + 1;
}
i = i + 1;
}
stoptime();
putint(ans);
putch(10);
return 0;
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,71 @@
int x;
const int N = 2010;
void mv(int n, int A[][N], int b[], int res[]){
int x, y;
y = 0;
x = 11;
int i, j;
i = 0;
while(i < n){
res[i] = 0;
i = i + 1;
}
i = 0;
j = 0;
while (i < n){
j = 0;
while (j < n){
if (A[i][j] == 0){
x = x * b[i] + b[j];
y = y - x;
}else{
res[i] = res[i] + A[i][j] * b[j];
}
j = j + 1;
}
i = i + 1;
}
}
int A[N][N];
int B[N];
int C[N];
int main(){
int n = getint();
int i, j;
i = 0;
while (i < n){
j = 0;
while (j < n){
A[i][j] = getint();
j = j + 1;
}
i = i + 1;
}
i = 0;
while (i < n){
B[i] = getint();
i = i + 1;
}
starttime();
i = 0;
while (i < 50){
mv(n, A, B, C);
mv(n, A, C, B);
i = i + 1;
}
stoptime();
putarray(n, C);
return 0;
}

File diff suppressed because one or more lines are too long

@ -0,0 +1,106 @@
const int base = 16;
int getMaxNum(int n, int arr[]){
int ret = 0;
int i = 0;
while (i < n){
if (arr[i] > ret) ret = arr[i];
i = i + 1;
}
return ret;
}
int getNumPos(int num, int pos){
int tmp = 1;
int i = 0;
while (i < pos){
num = num / base;
i = i + 1;
}
return num % base;
}
void radixSort(int bitround, int a[], int l, int r){
int head[base] = {};
int tail[base] = {};
int cnt[base] = {};
if (bitround == -1 || l + 1 >= r) return;
{
int i = l;
while (i < r){
cnt[getNumPos(a[i], bitround)]
= cnt[getNumPos(a[i], bitround)] + 1;
i = i + 1;
}
head[0] = l;
tail[0] = l + cnt[0];
i = 1;
while (i < base){
head[i] = tail[i - 1];
tail[i] = head[i] + cnt[i];
i = i + 1;
}
i = 0;
while (i < base){
while (head[i] < tail[i]){
int v = a[head[i]];
while (getNumPos(v, bitround) != i){
int t = v;
v = a[head[getNumPos(t, bitround)]];
a[head[getNumPos(t, bitround)]] = t;
head[getNumPos(t, bitround)] = head[getNumPos(t, bitround)] + 1;
}
a[head[i]] = v;
head[i] = head[i] + 1;
}
i = i + 1;
}
}
{
int i = l;
head[0] = l;
tail[0] = l + cnt[0];
i = 0;
while (i < base){
if (i > 0){
head[i] = tail[i - 1];
tail[i] = head[i] + cnt[i];
}
radixSort(bitround - 1, a, head[i], tail[i]);
i = i + 1;
}
}
return;
}
int a[30000010];
int ans;
int main(){
int n = getarray(a);
starttime();
radixSort(8, a, 0, n);
int i = 0;
while (i < n){
ans = ans + i * (a[i] % (2 + i));
i = i + 1;
}
if (ans < 0)
ans = -ans;
stoptime();
putint(ans);
putch(10);
return 0;
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,110 @@
int A[1024][1024];
int B[1024][1024];
int C[1024][1024];
int main() {
int T = getint(); // 矩阵规模
int R = getint(); // 重复次数
int i = 0;
while (i < T) {
if (i < T / 2) {
getarray(A[i]);
}
i = i + 1;
}
i = 0;
while (i < T) {
if (i >= T / 2) {
getarray(B[i]);
}
i = i + 1;
}
starttime();
i = 0;
while (i < T) {
if (i >= T / 2) {
int j = 0;
while (j < T) {
A[i][j] = -1;
j = j + 1;
}
}
i = i + 1;
}
i = 0;
while (i < T) {
if (i < T / 2) {
int j = 0;
while (j < T) {
B[i][j] = -1;
j = j + 1;
}
}
i = i + 1;
}
i = 0;
while (i < T) {
int j = 0;
while (j < T) {
C[i][j] = A[i][j] * 2 + B[i][j] * 3;
j = j + 1;
}
i = i + 1;
}
i = 0;
while (i < T) {
int j = 0;
while (j < T) {
int val = C[i][j];
val = val * val + 7;
val = val / 3;
C[i][j] = val;
j = j + 1;
}
i = i + 1;
}
i = 0;
while (i < T) {
int j = 0;
while (j < T) {
int k = 0;
int sum = 0;
while (k < T) {
sum = sum + C[i][k] * A[k][j];
k = k + 1;
}
A[i][j] = sum;
j = j + 1;
}
i = i + 1;
}
int total = 0;
int r = 0;
while (r < R) {
i = 0;
while (i < T) {
int j = 0;
while (j < T) {
total = total + A[i][j] * A[i][j];
j = j + 1;
}
i = i + 1;
}
r = r + 1;
}
stoptime();
putint(total);
putch(10);
return 0;
}

File diff suppressed because one or more lines are too long

@ -0,0 +1,82 @@
const int mod = 998244353;
int d;
int multiply(int a, int b){
if (b == 0) return 0;
if (b == 1) return a % mod;
int cur = multiply(a, b/2);
cur = (cur + cur) % mod;
if (b % 2 == 1) return (cur + a) % mod;
else return cur;
}
int power(int a, int b){
if (b == 0) return 1;
int cur = power(a, b/2);
cur = multiply(cur, cur);
if (b % 2 == 1) return multiply(cur, a);
else return cur;
}
const int maxlen = 2097152;
int temp[maxlen], a[maxlen], b[maxlen], c[maxlen];
int memmove(int dst[], int dst_pos, int src[], int len){
int i = 0;
while (i < len){
dst[dst_pos + i] = src[i];
i = i + 1;
}
return i;
}
int fft(int arr[], int begin_pos, int n, int w){
if (n == 1) return 1;
int i = 0;
while (i < n){
if (i % 2 == 0) temp[i / 2] = arr[i + begin_pos];
else temp[n / 2 + i / 2] = arr[i + begin_pos];
i = i + 1;
}
memmove(arr, begin_pos, temp, n);
fft(arr, begin_pos, n / 2, multiply(w, w));
fft(arr, begin_pos + n / 2, n / 2, multiply(w, w));
i = 0;
int wn = 1;
while (i < n / 2){
int x = arr[begin_pos + i];
int y = arr[begin_pos + i + n / 2];
arr[begin_pos + i] = (x + multiply(wn, y)) % mod;
arr[begin_pos + i + n / 2] = (x - multiply(wn, y) + mod) % mod;
wn = multiply(wn, w);
i = i + 1;
}
return 0;
}
int main(){
int n = getarray(a);
int m = getarray(b);
starttime();
d = 1;
while (d < n + m - 1){
d = d * 2;
}
fft(a, 0, d, power(3, (mod - 1) / d));
fft(b, 0, d, power(3, (mod - 1) / d));
int i = 0;
while (i < d){
a[i] = multiply(a[i], b[i]);
i = i + 1;
}
fft(a, 0, d, power(3, mod-1 - (mod-1)/d));
i = 0;
while (i < d){
a[i] = multiply(a[i], power(d, mod-2));
i = i + 1;
}
stoptime();
putarray(n + m - 1, a);
return 0;
}

@ -0,0 +1,51 @@
50 50 353434
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
.....................#..#.........................
.....................#..#.........................
...................##.##.##.......................
.....................#..#.........................
.....................#..#.........................
...................##.##.##.......................
.....................#..#.........................
.....................#..#.........................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................

@ -0,0 +1,112 @@
int sheet1[500][500] = {};
int sheet2[500][500] = {};
int active = 1;
int width;
int height;
int steps;
void read_map() {
width = getint();
height = getint();
// width <= 498, height <= 498
steps = getint();
getch();
int i = 1;
int j = 1;
while (j <= height) {
i = 1;
while (i <= width) {
int get = getch();
if (get == 35) {
sheet1[j][i] = 1;
} else {
sheet1[j][i] = 0;
}
i = i + 1;
}
// line feed
getch();
j = j + 1;
}
}
void put_map() {
int i = 1;
int j = 1;
while (j <= height) {
i = 1;
while (i <= width) {
if (sheet1[j][i] == 1) {
putch(35);
} else {
putch(46);
}
i = i + 1;
}
// line feed
putch(10);
j = j + 1;
}
}
void swap12() {
int i = 1;
int j = 1;
while (j <= height) {
i = 1;
while (i <= width) {
sheet1[j][i] = sheet2[j][i];
i = i + 1;
}
j = j + 1;
}
}
void step(int source[][500], int target[][500]) {
int i = 1;
int j = 1;
while (j <= height) {
i = 1;
while (i <= width) {
int alive_count = source[j - 1][i - 1] + source[j - 1][i] +
source[j - 1][i + 1] + source[j][i - 1] +
source[j][i + 1] + source[j + 1][i - 1] +
source[j + 1][i] + source[j + 1][i + 1];
if (source[j][i] == 1 && alive_count == 2 ) {
target[j][i] = 1;
} else if (alive_count == 3) {
target[j][i] = 1;
} else {
target[j][i] = 0;
}
i = i + 1;
}
j = j + 1;
}
}
int main() {
read_map();
starttime();
while (steps > 0) {
if (active == 1) {
step(sheet1, sheet2);
active = 2;
} else {
step(sheet2, sheet1);
active = 1;
}
steps = steps - 1;
}
stoptime();
if (active == 2) {
swap12();
}
put_map();
return 0;
}

@ -0,0 +1,331 @@
int func(int n) {
int sum = 0;
int i = 200;
int j = 0;
int s[100];
int m = 0;
while (m < 100){
s[m] = 0;
m=m+1;
}
while(j < n) {
if (i > 1){
s[1] = 1;
if (i > 2){
s[2] = 2;
if (i > 3){
s[3] = 3;
if (i > 4){
s[4] = 4;
if (i > 5){
s[5] = 5;
if (i > 6){
s[6] = 6;
if (i > 7){
s[7] = 7;
if (i > 8){
s[8] = 8;
if (i > 9){
s[9] = 9;
if (i > 10){
s[10] = 10;
if (i > 11){
s[11] = 11;
if (i > 12){
s[12] = 12;
if (i > 13){
s[13] = 13;
if (i > 14){
s[14] = 14;
if (i > 15){
s[15] = 15;
if (i > 16){
s[16] = 16;
if (i > 17){
s[17] = 17;
if (i > 18){
s[18] = 18;
if (i > 19){
s[19] = 19;
if (i > 20){
s[20] = 20;
if (i > 21){
s[21] = 21;
if (i > 22){
s[22] = 22;
if (i > 23){
s[23] = 23;
if (i > 24){
s[24] = 24;
if (i > 25){
s[25] = 25;
if (i > 26){
s[26] = 26;
if (i > 27){
s[27] = 27;
if (i > 28){
s[28] = 28;
if (i > 29){
s[29] = 29;
if (i > 30){
s[30] = 30;
if (i > 31){
s[31] = 31;
if (i > 32){
s[32] = 32;
if (i > 33){
s[33] = 33;
if (i > 34){
s[34] = 34;
if (i > 35){
s[35] = 35;
if (i > 36){
s[36] = 36;
if (i > 37){
s[37] = 37;
if (i > 38){
s[38] = 38;
if (i > 39){
s[39] = 39;
if (i > 40){
s[40] = 40;
if (i > 41){
s[41] = 41;
if (i > 42){
s[42] = 42;
if (i > 43){
s[43] = 43;
if (i > 44){
s[44] = 44;
if (i > 45){
s[45] = 45;
if (i > 46){
s[46] = 46;
if (i > 47){
s[47] = 47;
if (i > 48){
s[48] = 48;
if (i > 49){
s[49] = 49;
if (i > 50){
s[50] = 50;
if (i > 51){
s[51] = 51;
if (i > 52){
s[52] = 52;
if (i > 53){
s[53] = 53;
if (i > 54){
s[54] = 54;
if (i > 55){
s[55] = 55;
if (i > 56){
s[56] = 56;
if (i > 57){
s[57] = 57;
if (i > 58){
s[58] = 58;
if (i > 59){
s[59] = 59;
if (i > 60){
s[60] = 60;
if (i > 61){
s[61] = 61;
if (i > 62){
s[62] = 62;
if (i > 63){
s[63] = 63;
if (i > 64){
s[64] = 64;
if (i > 65){
s[65] = 65;
if (i > 66){
s[66] = 66;
if (i > 67){
s[67] = 67;
if (i > 68){
s[68] = 68;
if (i > 69){
s[69] = 69;
if (i > 70){
s[70] = 70;
if (i > 71){
s[71] = 71;
if (i > 72){
s[72] = 72;
if (i > 73){
s[73] = 73;
if (i > 74){
s[74] = 74;
if (i > 75){
s[75] = 75;
if (i > 76){
s[76] = 76;
if (i > 77){
s[77] = 77;
if (i > 78){
s[78] = 78;
if (i > 79){
s[79] = 79;
if (i > 80){
s[80] = 80;
if (i > 81){
s[81] = 81;
if (i > 82){
s[82] = 82;
if (i > 83){
s[83] = 83;
if (i > 84){
s[84] = 84;
if (i > 85){
s[85] = 85;
if (i > 86){
s[86] = 86;
if (i > 87){
s[87] = 87;
if (i > 88){
s[88] = 88;
if (i > 89){
s[89] = 89;
if (i > 90){
s[90] = 90;
if (i > 91){
s[91] = 91;
if (i > 92){
s[92] = 92;
if (i > 93){
s[93] = 93;
if (i > 94){
s[94] = 94;
if (i > 95){
s[95] = 95;
if (i > 96){
s[96] = 96;
if (i > 97){
s[97] = 97;
if (i > 98){
s[98] = 98;
if (i > 99){
s[99] = 99;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
j=j+1;
int m = 0;
while (m < 100){
sum = sum + s[m];
m=m+1;
}
sum = sum % 65535;
}
return sum;
}
int main() {
starttime();
int loopcount = getint();
putint(func(loopcount));
putch(10);
stoptime();
return 0;
}

@ -0,0 +1,49 @@
int COUNT = 500000;
float loop(float x[], float y[], int length) {
int i = 0;
float accumulator = 0.0;
while (i < length) {
accumulator = accumulator + x[i] * y[i];
i = i + 1;
}
return accumulator;
}
int main() {
int i = 0, j = 0;
int len = getint();
float x[4096];
float y[4096];
float total = 0.0;
float a = 0.0;
float b = 1.0;
starttime();
while ( i < COUNT) {
if (i % 10) {
a = 0.0;
b = 1.0;
} else {
a = a + 0.1;
b = b + 0.2;
}
while ( j < len) {
x[j] = a + j;
y[j] = b + j;
j = j + 1;
}
total = total + loop(x, y, len);
i = i + 1;
}
stoptime();
if ((total - 11442437121638400.000000) <=0.000001 || (total - 11442437121638400.000000) >= -0.000001) {
putint(0);
return 0;
}
else {
putint(1);
return 1;
}
}

@ -0,0 +1,2 @@
10000000
30 2 5 4 25 8 125 16 625 32 3125 2 5 4 25 8 125 16 625 32 3125 2 5 4 25 8 125 16 625 32 3125

@ -0,0 +1,51 @@
int matrix[20000000];
int a[100000];
int transpose(int n, int matrix[], int rowsize){
int colsize = n / rowsize;
int i = 0;
int j = 0;
while (i < colsize){
j = 0;
while (j < rowsize){
if (i < j){
j = j + 1;
continue;
}
int curr = matrix[i * rowsize + j];
matrix[j * colsize + i] = matrix[i * rowsize + j];
matrix[i * rowsize + j] = curr;
j = j + 1;
}
i = i + 1;
}
return -1;
}
int main(){
int n = getint();
int len = getarray(a);
starttime();
int i = 0;
while (i < n){
matrix[i] = i;
i = i + 1;
}
i = 0;
while (i < len){
transpose(n, matrix, a[i]);
i = i + 1;
}
int ans = 0;
i = 0;
while (i < len){
ans = ans + i * i * matrix[i];
i = i + 1;
}
if (ans < 0) ans = -ans;
stoptime();
putint(ans);
putch(10);
return 0;
}

@ -0,0 +1,85 @@
int func(int i, int j) {
return ((i+j) * (i+j+1) / 2 + i + 1);
}
float Vectordot(float v[], float u[], int n) {
int i = 0;
float sum = 0;
while (i < n) {
sum =sum+ v[i] * u[i];
i=i+1;
}
return sum;
}
void mult1(float v[], float out[],int n) {
int i = 0, j = 0;
float sum = 0;
while (i < n) {
while (j < n) {
sum =sum+ v[j] / func(i,j);
j=j+1;
}
out[i] = sum;
i=i+1;
}
}
void mult2(float v[], float out[], int n) {
int i = 0, j = 0;
float sum = 0;
while (i < n) {
while (j < n) {
sum =sum+ v[j] / func(j,i);
j=j+1;
}
out[i] = sum;
i=i+1;
}
}
void mult_combin(float v[], float out[], int n, float tmp[]) {
mult1(v, tmp, n);
mult2(tmp, out, n);
}
float temp = 1;
float my_sqrt(float input) {
while (temp - input / temp > 1e-6 || temp - input / temp < -1e-6){
temp = (temp+input/temp)/2;
}
return temp;
}
int main() {
int n = 100000;
if (n <= 0) {
n = 2000;
}
starttime();
float vectorA[100000], vectorB[100000], Vectortmp[100000];
int i;
while(i < n) {
vectorA[i] = 1;
i=i+1;
}
i = 0;
while(i < 1000) {
mult_combin(vectorA, vectorB, n, Vectortmp);
mult_combin(vectorB, vectorA, n, Vectortmp);
i=i+1;
}
stoptime();
float result = my_sqrt(Vectordot(vectorA,vectorB, n) / Vectordot(vectorB,vectorB,n));
if(result - 1.000000 <= 1e-6 && result - 1.000000 >= -1e-6){
putint(1);
}else{
putint(0);
}
putch(10);
return 0;
}

@ -0,0 +1,3 @@
int main(){
return 3;
}

@ -0,0 +1,8 @@
//test domain of global var define and local define
int a = 3;
int b = 5;
int main(){
int a = 5;
return a + b;
}

@ -0,0 +1,8 @@
//test local var define
int main(){
int a, b0, _c;
a = 1;
b0 = 2;
_c = 3;
return b0 + _c;
}

@ -0,0 +1,4 @@
int a[10][10];
int main(){
return 0;
}

@ -0,0 +1,9 @@
//test array define
int main(){
int a[4][2] = {};
int b[4][2] = {1, 2, 3, 4, 5, 6, 7, 8};
int c[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
int d[4][2] = {1, 2, {3}, {5}, 7 , 8};
int e[4][2] = {{d[2][1], c[2][1]}, {3, 4}, {5, 6}, {7, 8}};
return e[3][1] + e[0][0] + e[0][1] + a[2][0];
}

@ -0,0 +1,9 @@
int main(){
const int a[4][2] = {{1, 2}, {3, 4}, {}, 7};
int b[4][2] = {};
int c[4][2] = {1, 2, 3, 4, 5, 6, 7, 8};
int d[3 + 1][2] = {1, 2, {3}, {5}, a[3][0], 8};
int e[4][2][1] = {{d[2][1], {c[2][1]}}, {3, 4}, {5, 6}, {7, 8}};
return e[3][1][0] + e[0][0][0] + e[0][1][0] + d[3][0];
}

@ -0,0 +1,6 @@
//test const gloal var define
const int a = 10, b = 5;
int main(){
return b;
}

@ -0,0 +1,5 @@
//test const local var define
int main(){
const int a = 10, b = 5;
return b;
}

@ -0,0 +1,5 @@
const int a[5]={0,1,2,3,4};
int main(){
return a[4];
}

@ -0,0 +1,8 @@
int defn(){
return 4;
}
int main(){
int a=defn();
return a;
}

@ -0,0 +1,5 @@
//test addc
const int a = 10;
int main(){
return a + 5;
}

@ -0,0 +1,6 @@
//test subc
int main(){
int a;
a = 10;
return a - 2;
}

@ -0,0 +1,7 @@
//test mul
int main(){
int a, b;
a = 10;
b = 5;
return a * b;
}

@ -0,0 +1,5 @@
//test mulc
const int a = 5;
int main(){
return a * 5;
}

@ -0,0 +1,7 @@
//test div
int main(){
int a, b;
a = 10;
b = 5;
return a / b;
}

@ -0,0 +1,5 @@
//test divc
const int a = 10;
int main(){
return a / 5;
}

@ -0,0 +1,6 @@
//test mod
int main(){
int a;
a = 10;
return a / 3;
}

@ -0,0 +1,6 @@
//test rem
int main(){
int a;
a = 10;
return a % 3;
}

@ -0,0 +1,25 @@
// test if-else-if
int ifElseIf() {
int a;
a = 5;
int b;
b = 10;
if(a == 6 || b == 0xb) {
return a;
}
else {
if (b == 10 && a == 1)
a = 25;
else if (b == 10 && a == -5)
a = a + 15;
else
a = -+a;
}
return a;
}
int main(){
putint(ifElseIf());
return 0;
}

@ -0,0 +1,18 @@
// test if-if-else
int ififElse() {
int a;
a = 5;
int b;
b = 10;
if(a == 5)
if (b == 10)
a = 25;
else
a = a + 15;
return (a);
}
int main(){
return (ififElse());
}

@ -0,0 +1,18 @@
// test if-{if-else}
int if_ifElse_() {
int a;
a = 5;
int b;
b = 10;
if(a == 5){
if (b == 10)
a = 25;
else
a = a + 15;
}
return (a);
}
int main(){
return (if_ifElse_());
}

@ -0,0 +1,18 @@
// test if-{if}-else
int if_if_Else() {
int a;
a = 5;
int b;
b = 10;
if(a == 5){
if (b == 10)
a = 25;
}
else
a = a + 15;
return (a);
}
int main(){
return (if_if_Else());
}

@ -0,0 +1,31 @@
int get_one(int a) {
return 1;
}
int deepWhileBr(int a, int b) {
int c;
c = a + b;
while (c < 75) {
int d;
d = 42;
if (c < 100) {
c = c + d;
if (c > 99) {
int e;
e = d * 2;
if (get_one(0) == 1) {
c = e * 2;
}
}
}
}
return (c);
}
int main() {
int p;
p = 2;
p = deepWhileBr(p, p);
putint(p);
return 0;
}

@ -0,0 +1,18 @@
int doubleWhile() {
int i;
i = 5;
int j;
j = 7;
while (i < 100) {
i = i + 30;
while(j < 100){
j = j + 6;
}
j = j - 100;
}
return (j);
}
int main() {
return doubleWhile();
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save