[llvm-commits] [llvm] r167121 - /llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
Duncan Sands
baldrick at free.fr
Wed Oct 31 08:22:32 PDT 2012
Hi Hans, thanks for doing this.
> --- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
> +++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Wed Oct 31 10:14:39 2012
> @@ -3200,64 +3200,59 @@
> isa<UndefValue>(C);
> }
>
> +/// LookupConstant - If V is a Constant, return it. Otherwise, try to look up
> +/// its constant value in ConstantPool, returning NULL if it's not there.
NULL -> null
> +static Constant *LookupConstant(Value *V,
> + const SmallDenseMap<Value*, Constant*>& ConstantPool) {
> + if (Constant *C = dyn_cast<Constant>(V))
> + return C;
> + return ConstantPool.lookup(V);
> +}
> +
> /// ConstantFold - Try to fold instruction I into a constant. This works for
> /// simple instructions such as binary operations where both operands are
> /// constant or can be replaced by constants from the ConstantPool. Returns the
> /// resulting constant on success, NULL otherwise.
> -static Constant* ConstantFold(Instruction *I,
> +static Constant *ConstantFold(Instruction *I,
> const SmallDenseMap<Value*, Constant*>& ConstantPool) {
> if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
> - Constant *A = dyn_cast<Constant>(BO->getOperand(0));
> - if (!A) A = ConstantPool.lookup(BO->getOperand(0));
> - if (!A) return NULL;
> -
> - Constant *B = dyn_cast<Constant>(BO->getOperand(1));
> - if (!B) B = ConstantPool.lookup(BO->getOperand(1));
> - if (!B) return NULL;
> -
> - Constant *C = ConstantExpr::get(BO->getOpcode(), A, B);
> - return C;
> + Constant *A = LookupConstant(BO->getOperand(0), ConstantPool);
> + if (!A)
> + return 0;
> + Constant *B = LookupConstant(BO->getOperand(1), ConstantPool);
> + if (!B)
> + return 0;
> + return ConstantExpr::get(BO->getOpcode(), A, B);
> }
>
> if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
> - Constant *A = dyn_cast<Constant>(I->getOperand(0));
> - if (!A) A = ConstantPool.lookup(I->getOperand(0));
> - if (!A) return NULL;
> -
> - Constant *B = dyn_cast<Constant>(I->getOperand(1));
> - if (!B) B = ConstantPool.lookup(I->getOperand(1));
> - if (!B) return NULL;
> -
> - Constant *C = ConstantExpr::getCompare(Cmp->getPredicate(), A, B);
> - return C;
> + Constant *A = LookupConstant(I->getOperand(0), ConstantPool);
> + if (!A)
> + return 0;
> + Constant *B = LookupConstant(I->getOperand(1), ConstantPool);
> + if (!B)
> + return 0;
> + return ConstantExpr::getCompare(Cmp->getPredicate(), A, B);
> }
>
> if (SelectInst *Select = dyn_cast<SelectInst>(I)) {
> - Constant *A = dyn_cast<Constant>(Select->getCondition());
> - if (!A) A = ConstantPool.lookup(Select->getCondition());
> - if (!A) return NULL;
> -
> - Value *Res;
> - if (A->isAllOnesValue()) Res = Select->getTrueValue();
> - else if (A->isNullValue()) Res = Select->getFalseValue();
> - else return NULL;
> -
> - Constant *C = dyn_cast<Constant>(Res);
> - if (!C) C = ConstantPool.lookup(Res);
> - if (!C) return NULL;
> - return C;
> + Constant *A = LookupConstant(Select->getCondition(), ConstantPool);
> + if (!A)
> + return 0;
> + if (A->isAllOnesValue())
> + return LookupConstant(Select->getTrueValue(), ConstantPool);
> + if (A->isNullValue())
> + return LookupConstant(Select->getFalseValue(), ConstantPool);
Should have
return 0;
at this point in case the condition wasn't a constant integer.
Ciao, Duncan.
More information about the llvm-commits
mailing list