From addba5057b9707c9545fa1c24837719f8b3abe00 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Sun, 11 Jul 2010 20:36:29 +0000 Subject: [PATCH] If it's safe to speculatively execute load(alloca) the it's safe to execute load(gep(alloca)) where the gep is all-zeros. There's more we could do here but this is a common case. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@108101 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Instruction.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/VMCore/Instruction.cpp b/lib/VMCore/Instruction.cpp index e8d2814c44..d3f62d0d08 100644 --- a/lib/VMCore/Instruction.cpp +++ b/lib/VMCore/Instruction.cpp @@ -401,12 +401,20 @@ bool Instruction::isSafeToSpeculativelyExecute() const { return false; // Note that it is not safe to speculate into a malloc'd region because // malloc may return null. - if (isa(getOperand(0))) + // It's also not safe to follow a bitcast, for example: + // bitcast i8* (alloca i8) to i32* + // would result in a 4-byte load from a 1-byte alloca. + Value *Op0 = getOperand(0); + if (GEPOperator *GEP = dyn_cast(Op0)) { + // TODO: it's safe to do this for any GEP with constant indices that + // compute inside the allocated type, but not for any inbounds gep. + if (GEP->hasAllZeroIndices()) + Op0 = GEP->getPointerOperand(); + } + if (isa(Op0)) return true; if (GlobalVariable *GV = dyn_cast(getOperand(0))) return !GV->hasExternalWeakLinkage(); - // FIXME: Handle cases involving GEPs. We have to be careful because - // a load of a out-of-bounds GEP has undefined behavior. return false; } case Call: -- 2.11.4.GIT