Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
Reid Spencer | ce38beb | 2007-04-09 18:00:57 +0000 | [diff] [blame] | 20 | #include "llvm/ParameterAttributes.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ConstantRange.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 26 | unsigned CallSite::getCallingConv() const { |
| 27 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 28 | return CI->getCallingConv(); |
| 29 | else |
| 30 | return cast<InvokeInst>(I)->getCallingConv(); |
| 31 | } |
| 32 | void CallSite::setCallingConv(unsigned CC) { |
| 33 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 34 | CI->setCallingConv(CC); |
| 35 | else |
| 36 | cast<InvokeInst>(I)->setCallingConv(CC); |
| 37 | } |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 38 | const ParamAttrsList* CallSite::getParamAttrs() const { |
| 39 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 40 | return CI->getParamAttrs(); |
| 41 | else |
| 42 | return cast<InvokeInst>(I)->getParamAttrs(); |
| 43 | } |
| 44 | void CallSite::setParamAttrs(const ParamAttrsList *PAL) { |
| 45 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 46 | CI->setParamAttrs(PAL); |
| 47 | else |
| 48 | cast<InvokeInst>(I)->setParamAttrs(PAL); |
| 49 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 50 | bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const { |
| 51 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 52 | return CI->paramHasAttr(i, attr); |
| 53 | else |
| 54 | return cast<InvokeInst>(I)->paramHasAttr(i, attr); |
| 55 | } |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 56 | |
| 57 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 58 | |
| 59 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 61 | // TerminatorInst Class |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 64 | // Out of line virtual method, so the vtable, etc has a home. |
| 65 | TerminatorInst::~TerminatorInst() { |
| 66 | } |
| 67 | |
| 68 | // Out of line virtual method, so the vtable, etc has a home. |
| 69 | UnaryInstruction::~UnaryInstruction() { |
| 70 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | // PHINode Class |
| 75 | //===----------------------------------------------------------------------===// |
| 76 | |
| 77 | PHINode::PHINode(const PHINode &PN) |
| 78 | : Instruction(PN.getType(), Instruction::PHI, |
| 79 | new Use[PN.getNumOperands()], PN.getNumOperands()), |
| 80 | ReservedSpace(PN.getNumOperands()) { |
| 81 | Use *OL = OperandList; |
| 82 | for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { |
| 83 | OL[i].init(PN.getOperand(i), this); |
| 84 | OL[i+1].init(PN.getOperand(i+1), this); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | PHINode::~PHINode() { |
| 89 | delete [] OperandList; |
| 90 | } |
| 91 | |
| 92 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 93 | // predecessor basic block is deleted. |
| 94 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
| 95 | unsigned NumOps = getNumOperands(); |
| 96 | Use *OL = OperandList; |
| 97 | assert(Idx*2 < NumOps && "BB not in PHI node!"); |
| 98 | Value *Removed = OL[Idx*2]; |
| 99 | |
| 100 | // Move everything after this operand down. |
| 101 | // |
| 102 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 103 | // client might not expect this to happen. The code as it is thrashes the |
| 104 | // use/def lists, which is kinda lame. |
| 105 | for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) { |
| 106 | OL[i-2] = OL[i]; |
| 107 | OL[i-2+1] = OL[i+1]; |
| 108 | } |
| 109 | |
| 110 | // Nuke the last value. |
| 111 | OL[NumOps-2].set(0); |
| 112 | OL[NumOps-2+1].set(0); |
| 113 | NumOperands = NumOps-2; |
| 114 | |
| 115 | // If the PHI node is dead, because it has zero entries, nuke it now. |
| 116 | if (NumOps == 2 && DeletePHIIfEmpty) { |
| 117 | // If anyone is using this PHI, make them use a dummy value instead... |
| 118 | replaceAllUsesWith(UndefValue::get(getType())); |
| 119 | eraseFromParent(); |
| 120 | } |
| 121 | return Removed; |
| 122 | } |
| 123 | |
| 124 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 125 | /// list according to the following behavior: |
| 126 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 127 | /// of operation. This grows the number of ops by 1.5 times. |
| 128 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 129 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 130 | /// |
| 131 | void PHINode::resizeOperands(unsigned NumOps) { |
| 132 | if (NumOps == 0) { |
| 133 | NumOps = (getNumOperands())*3/2; |
| 134 | if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. |
| 135 | } else if (NumOps*2 > NumOperands) { |
| 136 | // No resize needed. |
| 137 | if (ReservedSpace >= NumOps) return; |
| 138 | } else if (NumOps == NumOperands) { |
| 139 | if (ReservedSpace == NumOps) return; |
| 140 | } else { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 141 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | ReservedSpace = NumOps; |
| 145 | Use *NewOps = new Use[NumOps]; |
| 146 | Use *OldOps = OperandList; |
| 147 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 148 | NewOps[i].init(OldOps[i], this); |
| 149 | OldOps[i].set(0); |
| 150 | } |
| 151 | delete [] OldOps; |
| 152 | OperandList = NewOps; |
| 153 | } |
| 154 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 155 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 156 | /// value, return the value, otherwise return null. |
| 157 | /// |
Chris Lattner | 1d8b248 | 2005-08-05 00:49:06 +0000 | [diff] [blame] | 158 | Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 159 | // If the PHI node only has one incoming value, eliminate the PHI node... |
| 160 | if (getNumIncomingValues() == 1) |
Chris Lattner | 6e709c1 | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 161 | if (getIncomingValue(0) != this) // not X = phi X |
| 162 | return getIncomingValue(0); |
| 163 | else |
| 164 | return UndefValue::get(getType()); // Self cycle is dead. |
| 165 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 166 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 167 | // the PHI node with the incoming value. |
| 168 | // |
| 169 | Value *InVal = 0; |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 170 | bool HasUndefInput = false; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 171 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 172 | if (isa<UndefValue>(getIncomingValue(i))) |
| 173 | HasUndefInput = true; |
| 174 | else if (getIncomingValue(i) != this) // Not the PHI node itself... |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 175 | if (InVal && getIncomingValue(i) != InVal) |
| 176 | return 0; // Not the same, bail out. |
| 177 | else |
| 178 | InVal = getIncomingValue(i); |
| 179 | |
| 180 | // The only case that could cause InVal to be null is if we have a PHI node |
| 181 | // that only has entries for itself. In this case, there is no entry into the |
| 182 | // loop, so kill the PHI. |
| 183 | // |
| 184 | if (InVal == 0) InVal = UndefValue::get(getType()); |
| 185 | |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 186 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 187 | // instruction, we cannot always return X as the result of the PHI node. Only |
| 188 | // do this if X is not an instruction (thus it must dominate the PHI block), |
| 189 | // or if the client is prepared to deal with this possibility. |
| 190 | if (HasUndefInput && !AllowNonDominatingInstruction) |
| 191 | if (Instruction *IV = dyn_cast<Instruction>(InVal)) |
| 192 | // If it's in the entry block, it dominates everything. |
Dan Gohman | dcb291f | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 193 | if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() || |
Chris Lattner | 37774af | 2005-08-05 01:03:27 +0000 | [diff] [blame] | 194 | isa<InvokeInst>(IV)) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 195 | return 0; // Cannot guarantee that InVal dominates this PHINode. |
| 196 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 197 | // All of the incoming values are the same, return the value now. |
| 198 | return InVal; |
| 199 | } |
| 200 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 201 | |
| 202 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 203 | // CallInst Implementation |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 206 | CallInst::~CallInst() { |
| 207 | delete [] OperandList; |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 208 | if (ParamAttrs) |
| 209 | ParamAttrs->dropRef(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 212 | void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 213 | ParamAttrs = 0; |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 214 | NumOperands = NumParams+1; |
| 215 | Use *OL = OperandList = new Use[NumParams+1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 216 | OL[0].init(Func, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 217 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 218 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 219 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 220 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 222 | assert((NumParams == FTy->getNumParams() || |
| 223 | (FTy->isVarArg() && NumParams > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 224 | "Calling a function with bad signature!"); |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 225 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 226 | assert((i >= FTy->getNumParams() || |
| 227 | FTy->getParamType(i) == Params[i]->getType()) && |
| 228 | "Calling a function with a bad signature!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 229 | OL[i+1].init(Params[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 230 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 233 | void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 234 | ParamAttrs = 0; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 235 | NumOperands = 3; |
| 236 | Use *OL = OperandList = new Use[3]; |
| 237 | OL[0].init(Func, this); |
| 238 | OL[1].init(Actual1, this); |
| 239 | OL[2].init(Actual2, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 240 | |
| 241 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 242 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 243 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 244 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 245 | assert((FTy->getNumParams() == 2 || |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 246 | (FTy->isVarArg() && FTy->getNumParams() < 2)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 247 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 248 | assert((0 >= FTy->getNumParams() || |
| 249 | FTy->getParamType(0) == Actual1->getType()) && |
| 250 | "Calling a function with a bad signature!"); |
| 251 | assert((1 >= FTy->getNumParams() || |
| 252 | FTy->getParamType(1) == Actual2->getType()) && |
| 253 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 256 | void CallInst::init(Value *Func, Value *Actual) { |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 257 | ParamAttrs = 0; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 258 | NumOperands = 2; |
| 259 | Use *OL = OperandList = new Use[2]; |
| 260 | OL[0].init(Func, this); |
| 261 | OL[1].init(Actual, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 262 | |
| 263 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 264 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 265 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 266 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 267 | assert((FTy->getNumParams() == 1 || |
| 268 | (FTy->isVarArg() && FTy->getNumParams() == 0)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 269 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 270 | assert((0 == FTy->getNumParams() || |
| 271 | FTy->getParamType(0) == Actual->getType()) && |
| 272 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 275 | void CallInst::init(Value *Func) { |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 276 | ParamAttrs = 0; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 277 | NumOperands = 1; |
| 278 | Use *OL = OperandList = new Use[1]; |
| 279 | OL[0].init(Func, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 280 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 281 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 282 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 283 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 284 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 285 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 286 | } |
| 287 | |
David Greene | 17a5dfe | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 288 | #if 0 |
| 289 | // Leave for llvm-gcc |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 290 | CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 291 | const std::string &Name, BasicBlock *InsertAtEnd) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 292 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
David Greene | 17a5dfe | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 293 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 294 | Instruction::Call, 0, 0, InsertAtEnd) { |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 295 | init(Func, Args, NumArgs); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 296 | setName(Name); |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 297 | } |
| 298 | CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs, |
| 299 | const std::string &Name, Instruction *InsertBefore) |
David Greene | 17a5dfe | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 300 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 301 | ->getElementType())->getReturnType(), |
| 302 | Instruction::Call, 0, 0, InsertBefore) { |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 303 | init(Func, Args, NumArgs); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 304 | setName(Name); |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 307 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 308 | const std::string &Name, Instruction *InsertBefore) |
| 309 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 310 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 311 | Instruction::Call, 0, 0, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 312 | init(Func, Actual1, Actual2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 313 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 317 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 318 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 319 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 320 | Instruction::Call, 0, 0, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 321 | init(Func, Actual1, Actual2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 322 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 323 | } |
David Greene | 17a5dfe | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 324 | #endif |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 325 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 326 | Instruction *InsertBefore) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 327 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 328 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 329 | Instruction::Call, 0, 0, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 330 | init(Func, Actual); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 331 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 335 | BasicBlock *InsertAtEnd) |
| 336 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 337 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 338 | Instruction::Call, 0, 0, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 339 | init(Func, Actual); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 340 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 341 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 342 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 343 | Instruction *InsertBefore) |
| 344 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 345 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 346 | Instruction::Call, 0, 0, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 347 | init(Func); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 348 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 352 | BasicBlock *InsertAtEnd) |
| 353 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 354 | ->getElementType())->getReturnType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 355 | Instruction::Call, 0, 0, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 356 | init(Func); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 357 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 360 | CallInst::CallInst(const CallInst &CI) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 361 | : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()], |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 362 | CI.getNumOperands()), |
| 363 | ParamAttrs(0) { |
| 364 | setParamAttrs(CI.getParamAttrs()); |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 365 | SubclassData = CI.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 366 | Use *OL = OperandList; |
| 367 | Use *InOL = CI.OperandList; |
| 368 | for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) |
| 369 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 372 | void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) { |
| 373 | if (ParamAttrs == newAttrs) |
| 374 | return; |
| 375 | |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 376 | if (ParamAttrs) |
| 377 | ParamAttrs->dropRef(); |
| 378 | |
| 379 | if (newAttrs) |
| 380 | newAttrs->addRef(); |
| 381 | |
| 382 | ParamAttrs = newAttrs; |
| 383 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 384 | |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 385 | bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const { |
| 386 | if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr)) |
| 387 | return true; |
| 388 | const Function *F = getCalledFunction(); |
| 389 | return F && F->getParamAttrs() && F->getParamAttrs()->paramHasAttr(i, attr); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 392 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 393 | //===----------------------------------------------------------------------===// |
| 394 | // InvokeInst Implementation |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 397 | InvokeInst::~InvokeInst() { |
| 398 | delete [] OperandList; |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 399 | if (ParamAttrs) |
| 400 | ParamAttrs->dropRef(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 403 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 404 | Value* const *Args, unsigned NumArgs) { |
Reid Spencer | ce38beb | 2007-04-09 18:00:57 +0000 | [diff] [blame] | 405 | ParamAttrs = 0; |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 406 | NumOperands = 3+NumArgs; |
| 407 | Use *OL = OperandList = new Use[3+NumArgs]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 408 | OL[0].init(Fn, this); |
| 409 | OL[1].init(IfNormal, this); |
| 410 | OL[2].init(IfException, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 411 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 412 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 413 | FTy = FTy; // silence warning. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 414 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 415 | assert((NumArgs == FTy->getNumParams()) || |
| 416 | (FTy->isVarArg() && NumArgs > FTy->getNumParams()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 417 | "Calling a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 418 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 419 | for (unsigned i = 0, e = NumArgs; i != e; i++) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 420 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 421 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 422 | "Invoking a function with a bad signature!"); |
| 423 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 424 | OL[i+3].init(Args[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 425 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 428 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 429 | : TerminatorInst(II.getType(), Instruction::Invoke, |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 430 | new Use[II.getNumOperands()], II.getNumOperands()), |
| 431 | ParamAttrs(0) { |
| 432 | setParamAttrs(II.getParamAttrs()); |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 433 | SubclassData = II.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 434 | Use *OL = OperandList, *InOL = II.OperandList; |
| 435 | for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) |
| 436 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 439 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 440 | return getSuccessor(idx); |
| 441 | } |
| 442 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 443 | return getNumSuccessors(); |
| 444 | } |
| 445 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 446 | return setSuccessor(idx, B); |
| 447 | } |
| 448 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 449 | void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) { |
| 450 | if (ParamAttrs == newAttrs) |
| 451 | return; |
| 452 | |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 453 | if (ParamAttrs) |
| 454 | ParamAttrs->dropRef(); |
| 455 | |
| 456 | if (newAttrs) |
| 457 | newAttrs->addRef(); |
| 458 | |
| 459 | ParamAttrs = newAttrs; |
| 460 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 461 | |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 462 | bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const { |
| 463 | if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr)) |
| 464 | return true; |
| 465 | const Function *F = getCalledFunction(); |
| 466 | return F && F->getParamAttrs() && F->getParamAttrs()->paramHasAttr(i, attr); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 469 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 470 | //===----------------------------------------------------------------------===// |
| 471 | // ReturnInst Implementation |
| 472 | //===----------------------------------------------------------------------===// |
| 473 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 474 | ReturnInst::ReturnInst(const ReturnInst &RI) |
| 475 | : TerminatorInst(Type::VoidTy, Instruction::Ret, |
| 476 | &RetVal, RI.getNumOperands()) { |
| 477 | if (RI.getNumOperands()) |
| 478 | RetVal.init(RI.RetVal, this); |
| 479 | } |
| 480 | |
| 481 | ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore) |
| 482 | : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) { |
| 483 | init(retVal); |
| 484 | } |
| 485 | ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd) |
| 486 | : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { |
| 487 | init(retVal); |
| 488 | } |
| 489 | ReturnInst::ReturnInst(BasicBlock *InsertAtEnd) |
| 490 | : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { |
| 491 | } |
| 492 | |
| 493 | |
| 494 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 495 | void ReturnInst::init(Value *retVal) { |
| 496 | if (retVal && retVal->getType() != Type::VoidTy) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 497 | assert(!isa<BasicBlock>(retVal) && |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 498 | "Cannot return basic block. Probably using the incorrect ctor"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 499 | NumOperands = 1; |
| 500 | RetVal.init(retVal, this); |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 504 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 505 | return getNumSuccessors(); |
| 506 | } |
| 507 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 508 | // Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 509 | // emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 510 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 511 | assert(0 && "ReturnInst has no successors!"); |
| 512 | } |
| 513 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 514 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
| 515 | assert(0 && "ReturnInst has no successors!"); |
| 516 | abort(); |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 521 | //===----------------------------------------------------------------------===// |
| 522 | // UnwindInst Implementation |
| 523 | //===----------------------------------------------------------------------===// |
| 524 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 525 | UnwindInst::UnwindInst(Instruction *InsertBefore) |
| 526 | : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) { |
| 527 | } |
| 528 | UnwindInst::UnwindInst(BasicBlock *InsertAtEnd) |
| 529 | : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) { |
| 530 | } |
| 531 | |
| 532 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 533 | unsigned UnwindInst::getNumSuccessorsV() const { |
| 534 | return getNumSuccessors(); |
| 535 | } |
| 536 | |
| 537 | void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 538 | assert(0 && "UnwindInst has no successors!"); |
| 539 | } |
| 540 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 541 | BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { |
| 542 | assert(0 && "UnwindInst has no successors!"); |
| 543 | abort(); |
| 544 | return 0; |
| 545 | } |
| 546 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 547 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 548 | // UnreachableInst Implementation |
| 549 | //===----------------------------------------------------------------------===// |
| 550 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 551 | UnreachableInst::UnreachableInst(Instruction *InsertBefore) |
| 552 | : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) { |
| 553 | } |
| 554 | UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd) |
| 555 | : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) { |
| 556 | } |
| 557 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 558 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 559 | return getNumSuccessors(); |
| 560 | } |
| 561 | |
| 562 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 563 | assert(0 && "UnwindInst has no successors!"); |
| 564 | } |
| 565 | |
| 566 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
| 567 | assert(0 && "UnwindInst has no successors!"); |
| 568 | abort(); |
| 569 | return 0; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 573 | // BranchInst Implementation |
| 574 | //===----------------------------------------------------------------------===// |
| 575 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 576 | void BranchInst::AssertOK() { |
| 577 | if (isConditional()) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 578 | assert(getCondition()->getType() == Type::Int1Ty && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 579 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 582 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
| 583 | : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) { |
| 584 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
| 585 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 586 | } |
| 587 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 588 | Instruction *InsertBefore) |
| 589 | : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) { |
| 590 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 591 | Ops[1].init(reinterpret_cast<Value*>(IfFalse), this); |
| 592 | Ops[2].init(Cond, this); |
| 593 | #ifndef NDEBUG |
| 594 | AssertOK(); |
| 595 | #endif |
| 596 | } |
| 597 | |
| 598 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
| 599 | : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) { |
| 600 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
| 601 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 602 | } |
| 603 | |
| 604 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 605 | BasicBlock *InsertAtEnd) |
| 606 | : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) { |
| 607 | Ops[0].init(reinterpret_cast<Value*>(IfTrue), this); |
| 608 | Ops[1].init(reinterpret_cast<Value*>(IfFalse), this); |
| 609 | Ops[2].init(Cond, this); |
| 610 | #ifndef NDEBUG |
| 611 | AssertOK(); |
| 612 | #endif |
| 613 | } |
| 614 | |
| 615 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 616 | BranchInst::BranchInst(const BranchInst &BI) : |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 617 | TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 618 | OperandList[0].init(BI.getOperand(0), this); |
| 619 | if (BI.getNumOperands() != 1) { |
| 620 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
| 621 | OperandList[1].init(BI.getOperand(1), this); |
| 622 | OperandList[2].init(BI.getOperand(2), this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 626 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 627 | return getSuccessor(idx); |
| 628 | } |
| 629 | unsigned BranchInst::getNumSuccessorsV() const { |
| 630 | return getNumSuccessors(); |
| 631 | } |
| 632 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 633 | setSuccessor(idx, B); |
| 634 | } |
| 635 | |
| 636 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 637 | //===----------------------------------------------------------------------===// |
| 638 | // AllocationInst Implementation |
| 639 | //===----------------------------------------------------------------------===// |
| 640 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 641 | static Value *getAISize(Value *Amt) { |
| 642 | if (!Amt) |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 643 | Amt = ConstantInt::get(Type::Int32Ty, 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 644 | else { |
| 645 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 646 | "Passed basic block into allocation size parameter! Use other ctor"); |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 647 | assert(Amt->getType() == Type::Int32Ty && |
Reid Spencer | 7e16e23 | 2007-01-26 06:30:34 +0000 | [diff] [blame] | 648 | "Malloc/Allocation array size is not a 32-bit integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 649 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 650 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 653 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 654 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 655 | Instruction *InsertBefore) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 656 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 657 | InsertBefore), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 658 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 659 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 660 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 663 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 664 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 665 | BasicBlock *InsertAtEnd) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 666 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 667 | InsertAtEnd), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 668 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 669 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 670 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 673 | // Out of line virtual method, so the vtable, etc has a home. |
| 674 | AllocationInst::~AllocationInst() { |
| 675 | } |
| 676 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 677 | bool AllocationInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 678 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
| 679 | return CI->getZExtValue() != 1; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 680 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | const Type *AllocationInst::getAllocatedType() const { |
| 684 | return getType()->getElementType(); |
| 685 | } |
| 686 | |
| 687 | AllocaInst::AllocaInst(const AllocaInst &AI) |
| 688 | : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 689 | Instruction::Alloca, AI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | MallocInst::MallocInst(const MallocInst &MI) |
| 693 | : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 694 | Instruction::Malloc, MI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | //===----------------------------------------------------------------------===// |
| 698 | // FreeInst Implementation |
| 699 | //===----------------------------------------------------------------------===// |
| 700 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 701 | void FreeInst::AssertOK() { |
| 702 | assert(isa<PointerType>(getOperand(0)->getType()) && |
| 703 | "Can not free something of nonpointer type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 707 | : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 708 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 712 | : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 713 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | |
| 717 | //===----------------------------------------------------------------------===// |
| 718 | // LoadInst Implementation |
| 719 | //===----------------------------------------------------------------------===// |
| 720 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 721 | void LoadInst::AssertOK() { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 722 | assert(isa<PointerType>(getOperand(0)->getType()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 723 | "Ptr must have pointer type."); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 727 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 728 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 729 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 730 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 731 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 732 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 736 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 737 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 738 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 739 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 740 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 741 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 745 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 746 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 747 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 748 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 749 | setAlignment(0); |
| 750 | AssertOK(); |
| 751 | setName(Name); |
| 752 | } |
| 753 | |
| 754 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 755 | unsigned Align, Instruction *InsertBef) |
| 756 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 757 | Load, Ptr, InsertBef) { |
| 758 | setVolatile(isVolatile); |
| 759 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 760 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 761 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 764 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 765 | unsigned Align, BasicBlock *InsertAE) |
| 766 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 767 | Load, Ptr, InsertAE) { |
| 768 | setVolatile(isVolatile); |
| 769 | setAlignment(Align); |
| 770 | AssertOK(); |
| 771 | setName(Name); |
| 772 | } |
| 773 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 774 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 775 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 776 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 777 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 778 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 779 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 780 | AssertOK(); |
| 781 | setName(Name); |
| 782 | } |
| 783 | |
| 784 | |
| 785 | |
| 786 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 787 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 788 | Load, Ptr, InsertBef) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 789 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 790 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 791 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 792 | if (Name && Name[0]) setName(Name); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 796 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 797 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 798 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 799 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 800 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 801 | if (Name && Name[0]) setName(Name); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 805 | Instruction *InsertBef) |
| 806 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 807 | Load, Ptr, InsertBef) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 808 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 809 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 810 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 811 | if (Name && Name[0]) setName(Name); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 815 | BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 816 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 817 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 818 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 819 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 820 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 821 | if (Name && Name[0]) setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 824 | void LoadInst::setAlignment(unsigned Align) { |
| 825 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 826 | SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); |
| 827 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 828 | |
| 829 | //===----------------------------------------------------------------------===// |
| 830 | // StoreInst Implementation |
| 831 | //===----------------------------------------------------------------------===// |
| 832 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 833 | void StoreInst::AssertOK() { |
| 834 | assert(isa<PointerType>(getOperand(1)->getType()) && |
| 835 | "Ptr must have pointer type!"); |
| 836 | assert(getOperand(0)->getType() == |
| 837 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 838 | && "Ptr must be a pointer to Val type!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 839 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 840 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 841 | |
| 842 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 843 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 844 | Ops[0].init(val, this); |
| 845 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 846 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 847 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 848 | AssertOK(); |
| 849 | } |
| 850 | |
| 851 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 852 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 853 | Ops[0].init(val, this); |
| 854 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 855 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 856 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 857 | AssertOK(); |
| 858 | } |
| 859 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 860 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 861 | Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 862 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 863 | Ops[0].init(val, this); |
| 864 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 865 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 866 | setAlignment(0); |
| 867 | AssertOK(); |
| 868 | } |
| 869 | |
| 870 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 871 | unsigned Align, Instruction *InsertBefore) |
| 872 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { |
| 873 | Ops[0].init(val, this); |
| 874 | Ops[1].init(addr, this); |
| 875 | setVolatile(isVolatile); |
| 876 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 877 | AssertOK(); |
| 878 | } |
| 879 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 880 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 881 | unsigned Align, BasicBlock *InsertAtEnd) |
| 882 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { |
| 883 | Ops[0].init(val, this); |
| 884 | Ops[1].init(addr, this); |
| 885 | setVolatile(isVolatile); |
| 886 | setAlignment(Align); |
| 887 | AssertOK(); |
| 888 | } |
| 889 | |
| 890 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 891 | BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 892 | : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 893 | Ops[0].init(val, this); |
| 894 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 895 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 896 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 897 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 900 | void StoreInst::setAlignment(unsigned Align) { |
| 901 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 902 | SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); |
| 903 | } |
| 904 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 905 | //===----------------------------------------------------------------------===// |
| 906 | // GetElementPtrInst Implementation |
| 907 | //===----------------------------------------------------------------------===// |
| 908 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 909 | void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) { |
| 910 | NumOperands = 1+NumIdx; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 911 | Use *OL = OperandList = new Use[NumOperands]; |
| 912 | OL[0].init(Ptr, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 913 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 914 | for (unsigned i = 0; i != NumIdx; ++i) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 915 | OL[i+1].init(Idx[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 918 | void GetElementPtrInst::init(Value *Ptr, Value *Idx) { |
| 919 | NumOperands = 2; |
| 920 | Use *OL = OperandList = new Use[2]; |
| 921 | OL[0].init(Ptr, this); |
| 922 | OL[1].init(Idx, this); |
| 923 | } |
| 924 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 925 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 926 | const std::string &Name, Instruction *InBe) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 927 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 928 | GetElementPtr, 0, 0, InBe) { |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 929 | init(Ptr, Idx); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 930 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 934 | const std::string &Name, BasicBlock *IAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 935 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 936 | GetElementPtr, 0, 0, IAE) { |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 937 | init(Ptr, Idx); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 938 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 941 | GetElementPtrInst::~GetElementPtrInst() { |
| 942 | delete[] OperandList; |
| 943 | } |
| 944 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 945 | // getIndexedType - Returns the type of the element that would be loaded with |
| 946 | // a load instruction with the specified parameters. |
| 947 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 948 | // A null type is returned if the indices are invalid for the specified |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 949 | // pointer type. |
| 950 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 951 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 952 | Value* const *Idxs, |
| 953 | unsigned NumIdx, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 954 | bool AllowCompositeLeaf) { |
| 955 | if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type! |
| 956 | |
| 957 | // Handle the special case of the empty set index set... |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 958 | if (NumIdx == 0) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 959 | if (AllowCompositeLeaf || |
| 960 | cast<PointerType>(Ptr)->getElementType()->isFirstClassType()) |
| 961 | return cast<PointerType>(Ptr)->getElementType(); |
| 962 | else |
| 963 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 964 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 965 | unsigned CurIdx = 0; |
| 966 | while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 967 | if (NumIdx == CurIdx) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 968 | if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; |
| 969 | return 0; // Can't load a whole structure or array!?!? |
| 970 | } |
| 971 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 972 | Value *Index = Idxs[CurIdx++]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 973 | if (isa<PointerType>(CT) && CurIdx != 1) |
| 974 | return 0; // Can only index into pointer types at the first index! |
| 975 | if (!CT->indexValid(Index)) return 0; |
| 976 | Ptr = CT->getTypeAtIndex(Index); |
| 977 | |
| 978 | // If the new type forwards to another type, then it is in the middle |
| 979 | // of being refined to another type (and hence, may have dropped all |
| 980 | // references to what it was using before). So, use the new forwarded |
| 981 | // type. |
| 982 | if (const Type * Ty = Ptr->getForwardedType()) { |
| 983 | Ptr = Ty; |
| 984 | } |
| 985 | } |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 986 | return CurIdx == NumIdx ? Ptr : 0; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 989 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 990 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 991 | if (!PTy) return 0; // Type isn't a pointer type! |
| 992 | |
| 993 | // Check the pointer index. |
| 994 | if (!PTy->indexValid(Idx)) return 0; |
| 995 | |
Chris Lattner | c223333 | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 996 | return PTy->getElementType(); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 999 | |
| 1000 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1001 | /// zeros. If so, the result pointer and the first operand have the same |
| 1002 | /// value, just potentially different types. |
| 1003 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1004 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1005 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1006 | if (!CI->isZero()) return false; |
| 1007 | } else { |
| 1008 | return false; |
| 1009 | } |
| 1010 | } |
| 1011 | return true; |
| 1012 | } |
| 1013 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1014 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1015 | /// constant integers. If so, the result pointer and the first operand have |
| 1016 | /// a constant offset between them. |
| 1017 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1018 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1019 | if (!isa<ConstantInt>(getOperand(i))) |
| 1020 | return false; |
| 1021 | } |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1025 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1026 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1027 | // ExtractElementInst Implementation |
| 1028 | //===----------------------------------------------------------------------===// |
| 1029 | |
| 1030 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1031 | const std::string &Name, |
| 1032 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1033 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1034 | ExtractElement, Ops, 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1035 | assert(isValidOperands(Val, Index) && |
| 1036 | "Invalid extractelement instruction operands!"); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1037 | Ops[0].init(Val, this); |
| 1038 | Ops[1].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1039 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1042 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 1043 | const std::string &Name, |
| 1044 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1045 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1046 | ExtractElement, Ops, 2, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1047 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1048 | assert(isValidOperands(Val, Index) && |
| 1049 | "Invalid extractelement instruction operands!"); |
| 1050 | Ops[0].init(Val, this); |
| 1051 | Ops[1].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1052 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1056 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1057 | const std::string &Name, |
| 1058 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1059 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1060 | ExtractElement, Ops, 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1061 | assert(isValidOperands(Val, Index) && |
| 1062 | "Invalid extractelement instruction operands!"); |
| 1063 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1064 | Ops[0].init(Val, this); |
| 1065 | Ops[1].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1066 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1069 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 1070 | const std::string &Name, |
| 1071 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1072 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1073 | ExtractElement, Ops, 2, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1074 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1075 | assert(isValidOperands(Val, Index) && |
| 1076 | "Invalid extractelement instruction operands!"); |
| 1077 | |
| 1078 | Ops[0].init(Val, this); |
| 1079 | Ops[1].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1080 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1084 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1085 | if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1086 | return false; |
| 1087 | return true; |
| 1088 | } |
| 1089 | |
| 1090 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1091 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1092 | // InsertElementInst Implementation |
| 1093 | //===----------------------------------------------------------------------===// |
| 1094 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 1095 | InsertElementInst::InsertElementInst(const InsertElementInst &IE) |
| 1096 | : Instruction(IE.getType(), InsertElement, Ops, 3) { |
| 1097 | Ops[0].init(IE.Ops[0], this); |
| 1098 | Ops[1].init(IE.Ops[1], this); |
| 1099 | Ops[2].init(IE.Ops[2], this); |
| 1100 | } |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1101 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1102 | const std::string &Name, |
| 1103 | Instruction *InsertBef) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1104 | : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1105 | assert(isValidOperands(Vec, Elt, Index) && |
| 1106 | "Invalid insertelement instruction operands!"); |
| 1107 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1108 | Ops[1].init(Elt, this); |
| 1109 | Ops[2].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1110 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1113 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 1114 | const std::string &Name, |
| 1115 | Instruction *InsertBef) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1116 | : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1117 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1118 | assert(isValidOperands(Vec, Elt, Index) && |
| 1119 | "Invalid insertelement instruction operands!"); |
| 1120 | Ops[0].init(Vec, this); |
| 1121 | Ops[1].init(Elt, this); |
| 1122 | Ops[2].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1123 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1127 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1128 | const std::string &Name, |
| 1129 | BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1130 | : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1131 | assert(isValidOperands(Vec, Elt, Index) && |
| 1132 | "Invalid insertelement instruction operands!"); |
| 1133 | |
| 1134 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1135 | Ops[1].init(Elt, this); |
| 1136 | Ops[2].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1137 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1140 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 1141 | const std::string &Name, |
| 1142 | BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1143 | : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1144 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1145 | assert(isValidOperands(Vec, Elt, Index) && |
| 1146 | "Invalid insertelement instruction operands!"); |
| 1147 | |
| 1148 | Ops[0].init(Vec, this); |
| 1149 | Ops[1].init(Elt, this); |
| 1150 | Ops[2].init(Index, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1151 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1154 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1155 | const Value *Index) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1156 | if (!isa<VectorType>(Vec->getType())) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1157 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1158 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1159 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1160 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1161 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1162 | if (Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1163 | return false; // Third operand of insertelement must be uint. |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
| 1167 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1168 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1169 | // ShuffleVectorInst Implementation |
| 1170 | //===----------------------------------------------------------------------===// |
| 1171 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 1172 | ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) |
| 1173 | : Instruction(SV.getType(), ShuffleVector, Ops, 3) { |
| 1174 | Ops[0].init(SV.Ops[0], this); |
| 1175 | Ops[1].init(SV.Ops[1], this); |
| 1176 | Ops[2].init(SV.Ops[2], this); |
| 1177 | } |
| 1178 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1179 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 1180 | const std::string &Name, |
| 1181 | Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1182 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1183 | assert(isValidOperands(V1, V2, Mask) && |
| 1184 | "Invalid shuffle vector instruction operands!"); |
| 1185 | Ops[0].init(V1, this); |
| 1186 | Ops[1].init(V2, this); |
| 1187 | Ops[2].init(Mask, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1188 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 1192 | const std::string &Name, |
| 1193 | BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1194 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1195 | assert(isValidOperands(V1, V2, Mask) && |
| 1196 | "Invalid shuffle vector instruction operands!"); |
| 1197 | |
| 1198 | Ops[0].init(V1, this); |
| 1199 | Ops[1].init(V2, this); |
| 1200 | Ops[2].init(Mask, this); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1201 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
| 1205 | const Value *Mask) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1206 | if (!isa<VectorType>(V1->getType())) return false; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1207 | if (V1->getType() != V2->getType()) return false; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1208 | if (!isa<VectorType>(Mask->getType()) || |
| 1209 | cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty || |
| 1210 | cast<VectorType>(Mask->getType())->getNumElements() != |
| 1211 | cast<VectorType>(V1->getType())->getNumElements()) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1212 | return false; |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
| 1216 | |
| 1217 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1218 | // BinaryOperator Class |
| 1219 | //===----------------------------------------------------------------------===// |
| 1220 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1221 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
| 1222 | const Type *Ty, const std::string &Name, |
| 1223 | Instruction *InsertBefore) |
| 1224 | : Instruction(Ty, iType, Ops, 2, InsertBefore) { |
| 1225 | Ops[0].init(S1, this); |
| 1226 | Ops[1].init(S2, this); |
| 1227 | init(iType); |
| 1228 | setName(Name); |
| 1229 | } |
| 1230 | |
| 1231 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
| 1232 | const Type *Ty, const std::string &Name, |
| 1233 | BasicBlock *InsertAtEnd) |
| 1234 | : Instruction(Ty, iType, Ops, 2, InsertAtEnd) { |
| 1235 | Ops[0].init(S1, this); |
| 1236 | Ops[1].init(S2, this); |
| 1237 | init(iType); |
| 1238 | setName(Name); |
| 1239 | } |
| 1240 | |
| 1241 | |
| 1242 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1243 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 1244 | LHS = LHS; RHS = RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1245 | assert(LHS->getType() == RHS->getType() && |
| 1246 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1247 | #ifndef NDEBUG |
| 1248 | switch (iType) { |
| 1249 | case Add: case Sub: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1250 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1251 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1252 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1253 | assert((getType()->isInteger() || getType()->isFloatingPoint() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1254 | isa<VectorType>(getType())) && |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1255 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1256 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1257 | case UDiv: |
| 1258 | case SDiv: |
| 1259 | assert(getType() == LHS->getType() && |
| 1260 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1261 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1262 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1263 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1264 | break; |
| 1265 | case FDiv: |
| 1266 | assert(getType() == LHS->getType() && |
| 1267 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1268 | assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) && |
| 1269 | cast<VectorType>(getType())->getElementType()->isFloatingPoint())) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1270 | && "Incorrect operand type (not floating point) for FDIV"); |
| 1271 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1272 | case URem: |
| 1273 | case SRem: |
| 1274 | assert(getType() == LHS->getType() && |
| 1275 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1276 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1277 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1278 | "Incorrect operand type (not integer) for S/UREM"); |
| 1279 | break; |
| 1280 | case FRem: |
| 1281 | assert(getType() == LHS->getType() && |
| 1282 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1283 | assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) && |
| 1284 | cast<VectorType>(getType())->getElementType()->isFloatingPoint())) |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1285 | && "Incorrect operand type (not floating point) for FREM"); |
| 1286 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1287 | case Shl: |
| 1288 | case LShr: |
| 1289 | case AShr: |
| 1290 | assert(getType() == LHS->getType() && |
| 1291 | "Shift operation should return same type as operands!"); |
| 1292 | assert(getType()->isInteger() && |
| 1293 | "Shift operation requires integer operands"); |
| 1294 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1295 | case And: case Or: |
| 1296 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1297 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1298 | "Logical operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1299 | assert((getType()->isInteger() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1300 | (isa<VectorType>(getType()) && |
| 1301 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1302 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1303 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1304 | default: |
| 1305 | break; |
| 1306 | } |
| 1307 | #endif |
| 1308 | } |
| 1309 | |
| 1310 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1311 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1312 | Instruction *InsertBefore) { |
| 1313 | assert(S1->getType() == S2->getType() && |
| 1314 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1315 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1319 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1320 | BasicBlock *InsertAtEnd) { |
| 1321 | BinaryOperator *Res = create(Op, S1, S2, Name); |
| 1322 | InsertAtEnd->getInstList().push_back(Res); |
| 1323 | return Res; |
| 1324 | } |
| 1325 | |
| 1326 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1327 | Instruction *InsertBefore) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1328 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1329 | return new BinaryOperator(Instruction::Sub, |
| 1330 | zero, Op, |
| 1331 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1335 | BasicBlock *InsertAtEnd) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1336 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1337 | return new BinaryOperator(Instruction::Sub, |
| 1338 | zero, Op, |
| 1339 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1343 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1344 | Constant *C; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1345 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1346 | C = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1347 | C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C)); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1348 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1349 | C = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1353 | Op->getType(), Name, InsertBefore); |
| 1354 | } |
| 1355 | |
| 1356 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1357 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1358 | Constant *AllOnes; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1359 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1360 | // Create a vector of all ones values. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1361 | Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1362 | AllOnes = |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1363 | ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt)); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1364 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1365 | AllOnes = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1369 | Op->getType(), Name, InsertAtEnd); |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | // isConstantAllOnes - Helper function for several functions below |
| 1374 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 1375 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 1376 | return CI->isAllOnesValue(); |
| 1377 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
| 1378 | return CV->isAllOnesValue(); |
| 1379 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | bool BinaryOperator::isNeg(const Value *V) { |
| 1383 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1384 | if (Bop->getOpcode() == Instruction::Sub) |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1385 | return Bop->getOperand(0) == |
| 1386 | ConstantExpr::getZeroValueForNegationExpr(Bop->getType()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1387 | return false; |
| 1388 | } |
| 1389 | |
| 1390 | bool BinaryOperator::isNot(const Value *V) { |
| 1391 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1392 | return (Bop->getOpcode() == Instruction::Xor && |
| 1393 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1394 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1398 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
| 1399 | assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!"); |
| 1400 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1403 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1404 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1407 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1408 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1409 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1410 | Value *Op0 = BO->getOperand(0); |
| 1411 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1412 | if (isConstantAllOnes(Op0)) return Op1; |
| 1413 | |
| 1414 | assert(isConstantAllOnes(Op1)); |
| 1415 | return Op0; |
| 1416 | } |
| 1417 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1418 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1419 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | |
| 1423 | // swapOperands - Exchange the two operands to this instruction. This |
| 1424 | // instruction is safe to use on any binary instruction and does not |
| 1425 | // modify the semantics of the instruction. If the instruction is |
| 1426 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1427 | // |
| 1428 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1429 | if (!isCommutative()) |
| 1430 | return true; // Can't commute operands |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1431 | std::swap(Ops[0], Ops[1]); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1432 | return false; |
| 1433 | } |
| 1434 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1435 | //===----------------------------------------------------------------------===// |
| 1436 | // CastInst Class |
| 1437 | //===----------------------------------------------------------------------===// |
| 1438 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1439 | // Just determine if this cast only deals with integral->integral conversion. |
| 1440 | bool CastInst::isIntegerCast() const { |
| 1441 | switch (getOpcode()) { |
| 1442 | default: return false; |
| 1443 | case Instruction::ZExt: |
| 1444 | case Instruction::SExt: |
| 1445 | case Instruction::Trunc: |
| 1446 | return true; |
| 1447 | case Instruction::BitCast: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1448 | return getOperand(0)->getType()->isInteger() && getType()->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1449 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1452 | bool CastInst::isLosslessCast() const { |
| 1453 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 1454 | if (getOpcode() != Instruction::BitCast) |
| 1455 | return false; |
| 1456 | |
| 1457 | // Identity cast is always lossless |
| 1458 | const Type* SrcTy = getOperand(0)->getType(); |
| 1459 | const Type* DstTy = getType(); |
| 1460 | if (SrcTy == DstTy) |
| 1461 | return true; |
| 1462 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1463 | // Pointer to pointer is always lossless. |
| 1464 | if (isa<PointerType>(SrcTy)) |
| 1465 | return isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1466 | return false; // Other types have no identity values |
| 1467 | } |
| 1468 | |
| 1469 | /// This function determines if the CastInst does not require any bits to be |
| 1470 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 1471 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 1472 | /// example, the following are all no-op casts: |
| 1473 | /// # bitcast uint %X, int |
| 1474 | /// # bitcast uint* %x, sbyte* |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1475 | /// # bitcast vector< 2 x int > %x, vector< 4 x short> |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1476 | /// # ptrtoint uint* %x, uint ; on 32-bit plaforms only |
| 1477 | /// @brief Determine if a cast is a no-op. |
| 1478 | bool CastInst::isNoopCast(const Type *IntPtrTy) const { |
| 1479 | switch (getOpcode()) { |
| 1480 | default: |
| 1481 | assert(!"Invalid CastOp"); |
| 1482 | case Instruction::Trunc: |
| 1483 | case Instruction::ZExt: |
| 1484 | case Instruction::SExt: |
| 1485 | case Instruction::FPTrunc: |
| 1486 | case Instruction::FPExt: |
| 1487 | case Instruction::UIToFP: |
| 1488 | case Instruction::SIToFP: |
| 1489 | case Instruction::FPToUI: |
| 1490 | case Instruction::FPToSI: |
| 1491 | return false; // These always modify bits |
| 1492 | case Instruction::BitCast: |
| 1493 | return true; // BitCast never modifies bits. |
| 1494 | case Instruction::PtrToInt: |
| 1495 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1496 | getType()->getPrimitiveSizeInBits(); |
| 1497 | case Instruction::IntToPtr: |
| 1498 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1499 | getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | /// This function determines if a pair of casts can be eliminated and what |
| 1504 | /// opcode should be used in the elimination. This assumes that there are two |
| 1505 | /// instructions like this: |
| 1506 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 1507 | /// * %S = secondOpcode MidTy %F to DstTy |
| 1508 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 1509 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 1510 | /// If no such cast is permited, the function returns 0. |
| 1511 | unsigned CastInst::isEliminableCastPair( |
| 1512 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
| 1513 | const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy) |
| 1514 | { |
| 1515 | // Define the 144 possibilities for these two cast instructions. The values |
| 1516 | // in this matrix determine what to do in a given situation and select the |
| 1517 | // case in the switch below. The rows correspond to firstOp, the columns |
| 1518 | // correspond to secondOp. In looking at the table below, keep in mind |
| 1519 | // the following cast properties: |
| 1520 | // |
| 1521 | // Size Compare Source Destination |
| 1522 | // Operator Src ? Size Type Sign Type Sign |
| 1523 | // -------- ------------ ------------------- --------------------- |
| 1524 | // TRUNC > Integer Any Integral Any |
| 1525 | // ZEXT < Integral Unsigned Integer Any |
| 1526 | // SEXT < Integral Signed Integer Any |
| 1527 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 1528 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 1529 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 1530 | // SITOFP n/a Integral Signed FloatPt n/a |
| 1531 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 1532 | // FPEXT < FloatPt n/a FloatPt n/a |
| 1533 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 1534 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
| 1535 | // BITCONVERT = FirstClass n/a FirstClass n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1536 | // |
| 1537 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
| 1538 | // For example, we could merge "fptoui double to uint" + "zext uint to ulong", |
| 1539 | // into "fptoui double to ulong", but this loses information about the range |
| 1540 | // of the produced value (we no longer know the top-part is all zeros). |
| 1541 | // Further this conversion is often much more expensive for typical hardware, |
| 1542 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 1543 | // same reason. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1544 | const unsigned numCastOps = |
| 1545 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 1546 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 1547 | // T F F U S F F P I B -+ |
| 1548 | // R Z S P P I I T P 2 N T | |
| 1549 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 1550 | // N X X U S F F N X N 2 V | |
| 1551 | // C T T I I P P C T T P T -+ |
| 1552 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 1553 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 1554 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1555 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 1556 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1557 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 1558 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 1559 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 1560 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 1561 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 1562 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 1563 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 1564 | }; |
| 1565 | |
| 1566 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 1567 | [secondOp-Instruction::CastOpsBegin]; |
| 1568 | switch (ElimCase) { |
| 1569 | case 0: |
| 1570 | // categorically disallowed |
| 1571 | return 0; |
| 1572 | case 1: |
| 1573 | // allowed, use first cast's opcode |
| 1574 | return firstOp; |
| 1575 | case 2: |
| 1576 | // allowed, use second cast's opcode |
| 1577 | return secondOp; |
| 1578 | case 3: |
| 1579 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1580 | // is integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1581 | if (DstTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1582 | return firstOp; |
| 1583 | return 0; |
| 1584 | case 4: |
| 1585 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1586 | // is floating point |
| 1587 | if (DstTy->isFloatingPoint()) |
| 1588 | return firstOp; |
| 1589 | return 0; |
| 1590 | case 5: |
| 1591 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1592 | // is an integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1593 | if (SrcTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1594 | return secondOp; |
| 1595 | return 0; |
| 1596 | case 6: |
| 1597 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1598 | // is a floating point |
| 1599 | if (SrcTy->isFloatingPoint()) |
| 1600 | return secondOp; |
| 1601 | return 0; |
| 1602 | case 7: { |
| 1603 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
| 1604 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1605 | unsigned MidSize = MidTy->getPrimitiveSizeInBits(); |
| 1606 | if (MidSize >= PtrSize) |
| 1607 | return Instruction::BitCast; |
| 1608 | return 0; |
| 1609 | } |
| 1610 | case 8: { |
| 1611 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 1612 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 1613 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
| 1614 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1615 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1616 | if (SrcSize == DstSize) |
| 1617 | return Instruction::BitCast; |
| 1618 | else if (SrcSize < DstSize) |
| 1619 | return firstOp; |
| 1620 | return secondOp; |
| 1621 | } |
| 1622 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 1623 | return Instruction::ZExt; |
| 1624 | case 10: |
| 1625 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 1626 | // the same as the original, in which case its just a bitcast |
| 1627 | if (SrcTy == DstTy) |
| 1628 | return Instruction::BitCast; |
| 1629 | return 0; // If the types are not the same we can't eliminate it. |
| 1630 | case 11: |
| 1631 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 1632 | // is a pointer to pointer cast. |
| 1633 | if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy)) |
| 1634 | return secondOp; |
| 1635 | return 0; |
| 1636 | case 12: |
| 1637 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
| 1638 | if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy)) |
| 1639 | return firstOp; |
| 1640 | return 0; |
| 1641 | case 13: { |
| 1642 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
| 1643 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1644 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1645 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1646 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 1647 | return Instruction::BitCast; |
| 1648 | return 0; |
| 1649 | } |
| 1650 | case 99: |
| 1651 | // cast combination can't happen (error in input). This is for all cases |
| 1652 | // where the MidTy is not the same for the two cast instructions. |
| 1653 | assert(!"Invalid Cast Combination"); |
| 1654 | return 0; |
| 1655 | default: |
| 1656 | assert(!"Error in CastResults table!!!"); |
| 1657 | return 0; |
| 1658 | } |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1663 | const std::string &Name, Instruction *InsertBefore) { |
| 1664 | // Construct and return the appropriate CastInst subclass |
| 1665 | switch (op) { |
| 1666 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 1667 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 1668 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 1669 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 1670 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 1671 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 1672 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 1673 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 1674 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 1675 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 1676 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 1677 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 1678 | default: |
| 1679 | assert(!"Invalid opcode provided"); |
| 1680 | } |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
| 1684 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1685 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 1686 | // Construct and return the appropriate CastInst subclass |
| 1687 | switch (op) { |
| 1688 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 1689 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 1690 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 1691 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 1692 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 1693 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1694 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1695 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 1696 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 1697 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 1698 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 1699 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 1700 | default: |
| 1701 | assert(!"Invalid opcode provided"); |
| 1702 | } |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1706 | CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, |
| 1707 | const std::string &Name, |
| 1708 | Instruction *InsertBefore) { |
| 1709 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1710 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1711 | return create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
| 1712 | } |
| 1713 | |
| 1714 | CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, |
| 1715 | const std::string &Name, |
| 1716 | BasicBlock *InsertAtEnd) { |
| 1717 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1718 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1719 | return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
| 1720 | } |
| 1721 | |
| 1722 | CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, |
| 1723 | const std::string &Name, |
| 1724 | Instruction *InsertBefore) { |
| 1725 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1726 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1727 | return create(Instruction::SExt, S, Ty, Name, InsertBefore); |
| 1728 | } |
| 1729 | |
| 1730 | CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, |
| 1731 | const std::string &Name, |
| 1732 | BasicBlock *InsertAtEnd) { |
| 1733 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1734 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1735 | return create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
| 1736 | } |
| 1737 | |
| 1738 | CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, |
| 1739 | const std::string &Name, |
| 1740 | Instruction *InsertBefore) { |
| 1741 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1742 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1743 | return create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
| 1744 | } |
| 1745 | |
| 1746 | CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, |
| 1747 | const std::string &Name, |
| 1748 | BasicBlock *InsertAtEnd) { |
| 1749 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1750 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1751 | return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
| 1752 | } |
| 1753 | |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1754 | CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, |
| 1755 | const std::string &Name, |
| 1756 | BasicBlock *InsertAtEnd) { |
| 1757 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1758 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1759 | "Invalid cast"); |
| 1760 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1761 | if (Ty->isInteger()) |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1762 | return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 1763 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1764 | } |
| 1765 | |
| 1766 | /// @brief Create a BitCast or a PtrToInt cast instruction |
| 1767 | CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, |
| 1768 | const std::string &Name, |
| 1769 | Instruction *InsertBefore) { |
| 1770 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1771 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1772 | "Invalid cast"); |
| 1773 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1774 | if (Ty->isInteger()) |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1775 | return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 1776 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1777 | } |
| 1778 | |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1779 | CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, |
| 1780 | bool isSigned, const std::string &Name, |
| 1781 | Instruction *InsertBefore) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1782 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1783 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1784 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1785 | Instruction::CastOps opcode = |
| 1786 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1787 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1788 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1789 | return create(opcode, C, Ty, Name, InsertBefore); |
| 1790 | } |
| 1791 | |
| 1792 | CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, |
| 1793 | bool isSigned, const std::string &Name, |
| 1794 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1795 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1796 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1797 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1798 | Instruction::CastOps opcode = |
| 1799 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1800 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1801 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1802 | return create(opcode, C, Ty, Name, InsertAtEnd); |
| 1803 | } |
| 1804 | |
| 1805 | CastInst *CastInst::createFPCast(Value *C, const Type *Ty, |
| 1806 | const std::string &Name, |
| 1807 | Instruction *InsertBefore) { |
| 1808 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1809 | "Invalid cast"); |
| 1810 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1811 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1812 | Instruction::CastOps opcode = |
| 1813 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1814 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
| 1815 | return create(opcode, C, Ty, Name, InsertBefore); |
| 1816 | } |
| 1817 | |
| 1818 | CastInst *CastInst::createFPCast(Value *C, const Type *Ty, |
| 1819 | const std::string &Name, |
| 1820 | BasicBlock *InsertAtEnd) { |
| 1821 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1822 | "Invalid cast"); |
| 1823 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1824 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1825 | Instruction::CastOps opcode = |
| 1826 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1827 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
| 1828 | return create(opcode, C, Ty, Name, InsertAtEnd); |
| 1829 | } |
| 1830 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1831 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 1832 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1833 | // logic in the castIsValid function below. This axiom should hold: |
| 1834 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 1835 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1836 | // casting opcode for the arguments passed to it. |
| 1837 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1838 | CastInst::getCastOpcode( |
| 1839 | const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1840 | // Get the bit sizes, we'll need these |
| 1841 | const Type *SrcTy = Src->getType(); |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1842 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector |
| 1843 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1844 | |
| 1845 | // Run through the possibilities ... |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1846 | if (DestTy->isInteger()) { // Casting to integral |
| 1847 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1848 | if (DestBits < SrcBits) |
| 1849 | return Trunc; // int -> smaller int |
| 1850 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1851 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1852 | return SExt; // signed -> SEXT |
| 1853 | else |
| 1854 | return ZExt; // unsigned -> ZEXT |
| 1855 | } else { |
| 1856 | return BitCast; // Same size, No-op cast |
| 1857 | } |
| 1858 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1859 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1860 | return FPToSI; // FP -> sint |
| 1861 | else |
| 1862 | return FPToUI; // FP -> uint |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1863 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1864 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1865 | "Casting vector to integer of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1866 | return BitCast; // Same size, no-op cast |
| 1867 | } else { |
| 1868 | assert(isa<PointerType>(SrcTy) && |
| 1869 | "Casting from a value that is not first-class type"); |
| 1870 | return PtrToInt; // ptr -> int |
| 1871 | } |
| 1872 | } else if (DestTy->isFloatingPoint()) { // Casting to floating pt |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1873 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1874 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1875 | return SIToFP; // sint -> FP |
| 1876 | else |
| 1877 | return UIToFP; // uint -> FP |
| 1878 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
| 1879 | if (DestBits < SrcBits) { |
| 1880 | return FPTrunc; // FP -> smaller FP |
| 1881 | } else if (DestBits > SrcBits) { |
| 1882 | return FPExt; // FP -> larger FP |
| 1883 | } else { |
| 1884 | return BitCast; // same size, no-op cast |
| 1885 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1886 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1887 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1888 | "Casting vector to floating point of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1889 | return BitCast; // same size, no-op cast |
| 1890 | } else { |
| 1891 | assert(0 && "Casting pointer or non-first class to float"); |
| 1892 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1893 | } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
| 1894 | if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1895 | assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1896 | "Casting vector to vector of different widths"); |
| 1897 | return BitCast; // vector -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1898 | } else if (DestPTy->getBitWidth() == SrcBits) { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1899 | return BitCast; // float/int -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1900 | } else { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1901 | assert(!"Illegal cast to vector (wrong type or size)"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1902 | } |
| 1903 | } else if (isa<PointerType>(DestTy)) { |
| 1904 | if (isa<PointerType>(SrcTy)) { |
| 1905 | return BitCast; // ptr -> ptr |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1906 | } else if (SrcTy->isInteger()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1907 | return IntToPtr; // int -> ptr |
| 1908 | } else { |
| 1909 | assert(!"Casting pointer to other than pointer or int"); |
| 1910 | } |
| 1911 | } else { |
| 1912 | assert(!"Casting to type that is not first-class"); |
| 1913 | } |
| 1914 | |
| 1915 | // If we fall through to here we probably hit an assertion cast above |
| 1916 | // and assertions are not turned on. Anything we return is an error, so |
| 1917 | // BitCast is as good a choice as any. |
| 1918 | return BitCast; |
| 1919 | } |
| 1920 | |
| 1921 | //===----------------------------------------------------------------------===// |
| 1922 | // CastInst SubClass Constructors |
| 1923 | //===----------------------------------------------------------------------===// |
| 1924 | |
| 1925 | /// Check that the construction parameters for a CastInst are correct. This |
| 1926 | /// could be broken out into the separate constructors but it is useful to have |
| 1927 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 1928 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1929 | bool |
| 1930 | CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1931 | |
| 1932 | // Check for type sanity on the arguments |
| 1933 | const Type *SrcTy = S->getType(); |
| 1934 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType()) |
| 1935 | return false; |
| 1936 | |
| 1937 | // Get the size of the types in bits, we'll need this later |
| 1938 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1939 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
| 1940 | |
| 1941 | // Switch on the opcode provided |
| 1942 | switch (op) { |
| 1943 | default: return false; // This is an input error |
| 1944 | case Instruction::Trunc: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1945 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1946 | case Instruction::ZExt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1947 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1948 | case Instruction::SExt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1949 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1950 | case Instruction::FPTrunc: |
| 1951 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1952 | SrcBitSize > DstBitSize; |
| 1953 | case Instruction::FPExt: |
| 1954 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1955 | SrcBitSize < DstBitSize; |
| 1956 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1957 | case Instruction::SIToFP: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1958 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 1959 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
| 1960 | return SVTy->getElementType()->isInteger() && |
| 1961 | DVTy->getElementType()->isFloatingPoint() && |
| 1962 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 1963 | } |
| 1964 | } |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1965 | return SrcTy->isInteger() && DstTy->isFloatingPoint(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1966 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1967 | case Instruction::FPToSI: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1968 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 1969 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
| 1970 | return SVTy->getElementType()->isFloatingPoint() && |
| 1971 | DVTy->getElementType()->isInteger() && |
| 1972 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 1973 | } |
| 1974 | } |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1975 | return SrcTy->isFloatingPoint() && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1976 | case Instruction::PtrToInt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1977 | return isa<PointerType>(SrcTy) && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1978 | case Instruction::IntToPtr: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1979 | return SrcTy->isInteger() && isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1980 | case Instruction::BitCast: |
| 1981 | // BitCast implies a no-op cast of type only. No bits change. |
| 1982 | // However, you can't cast pointers to anything but pointers. |
| 1983 | if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy)) |
| 1984 | return false; |
| 1985 | |
| 1986 | // Now we know we're not dealing with a pointer/non-poiner mismatch. In all |
| 1987 | // these cases, the cast is okay if the source and destination bit widths |
| 1988 | // are identical. |
| 1989 | return SrcBitSize == DstBitSize; |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | TruncInst::TruncInst( |
| 1994 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1995 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1996 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | TruncInst::TruncInst( |
| 2000 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2001 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2002 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | ZExtInst::ZExtInst( |
| 2006 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2007 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2008 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | ZExtInst::ZExtInst( |
| 2012 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2013 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2014 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2015 | } |
| 2016 | SExtInst::SExtInst( |
| 2017 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2018 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2019 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 2022 | SExtInst::SExtInst( |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2023 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2024 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2025 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | FPTruncInst::FPTruncInst( |
| 2029 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2030 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2031 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | FPTruncInst::FPTruncInst( |
| 2035 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2036 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2037 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | FPExtInst::FPExtInst( |
| 2041 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2042 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2043 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | FPExtInst::FPExtInst( |
| 2047 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2048 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2049 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | UIToFPInst::UIToFPInst( |
| 2053 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2054 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2055 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | UIToFPInst::UIToFPInst( |
| 2059 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2060 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2061 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | SIToFPInst::SIToFPInst( |
| 2065 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2066 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2067 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | SIToFPInst::SIToFPInst( |
| 2071 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2072 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2073 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | FPToUIInst::FPToUIInst( |
| 2077 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2078 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2079 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | FPToUIInst::FPToUIInst( |
| 2083 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2084 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2085 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | FPToSIInst::FPToSIInst( |
| 2089 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2090 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2091 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | FPToSIInst::FPToSIInst( |
| 2095 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2096 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2097 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | PtrToIntInst::PtrToIntInst( |
| 2101 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2102 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2103 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | PtrToIntInst::PtrToIntInst( |
| 2107 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2108 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2109 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | IntToPtrInst::IntToPtrInst( |
| 2113 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2114 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2115 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | IntToPtrInst::IntToPtrInst( |
| 2119 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2120 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2121 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | BitCastInst::BitCastInst( |
| 2125 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2126 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2127 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | BitCastInst::BitCastInst( |
| 2131 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2132 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2133 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2134 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 2135 | |
| 2136 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2137 | // CmpInst Classes |
| 2138 | //===----------------------------------------------------------------------===// |
| 2139 | |
| 2140 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 2141 | const std::string &Name, Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2142 | : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2143 | Ops[0].init(LHS, this); |
| 2144 | Ops[1].init(RHS, this); |
| 2145 | SubclassData = predicate; |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2146 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2147 | if (op == Instruction::ICmp) { |
| 2148 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 2149 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 2150 | "Invalid ICmp predicate value"); |
| 2151 | const Type* Op0Ty = getOperand(0)->getType(); |
| 2152 | const Type* Op1Ty = getOperand(1)->getType(); |
| 2153 | assert(Op0Ty == Op1Ty && |
| 2154 | "Both operands to ICmp instruction are not of the same type!"); |
| 2155 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2156 | assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2157 | "Invalid operand types for ICmp instruction"); |
| 2158 | return; |
| 2159 | } |
| 2160 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 2161 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 2162 | "Invalid FCmp predicate value"); |
| 2163 | const Type* Op0Ty = getOperand(0)->getType(); |
| 2164 | const Type* Op1Ty = getOperand(1)->getType(); |
| 2165 | assert(Op0Ty == Op1Ty && |
| 2166 | "Both operands to FCmp instruction are not of the same type!"); |
| 2167 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2168 | assert(Op0Ty->isFloatingPoint() && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2169 | "Invalid operand types for FCmp instruction"); |
| 2170 | } |
| 2171 | |
| 2172 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 2173 | const std::string &Name, BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2174 | : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2175 | Ops[0].init(LHS, this); |
| 2176 | Ops[1].init(RHS, this); |
| 2177 | SubclassData = predicate; |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2178 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2179 | if (op == Instruction::ICmp) { |
| 2180 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 2181 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 2182 | "Invalid ICmp predicate value"); |
| 2183 | |
| 2184 | const Type* Op0Ty = getOperand(0)->getType(); |
| 2185 | const Type* Op1Ty = getOperand(1)->getType(); |
| 2186 | assert(Op0Ty == Op1Ty && |
| 2187 | "Both operands to ICmp instruction are not of the same type!"); |
| 2188 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2189 | assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2190 | "Invalid operand types for ICmp instruction"); |
| 2191 | return; |
| 2192 | } |
| 2193 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 2194 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 2195 | "Invalid FCmp predicate value"); |
| 2196 | const Type* Op0Ty = getOperand(0)->getType(); |
| 2197 | const Type* Op1Ty = getOperand(1)->getType(); |
| 2198 | assert(Op0Ty == Op1Ty && |
| 2199 | "Both operands to FCmp instruction are not of the same type!"); |
| 2200 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2201 | assert(Op0Ty->isFloatingPoint() && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2202 | "Invalid operand types for FCmp instruction"); |
| 2203 | } |
| 2204 | |
| 2205 | CmpInst * |
| 2206 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 2207 | const std::string &Name, Instruction *InsertBefore) { |
| 2208 | if (Op == Instruction::ICmp) { |
| 2209 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 2210 | InsertBefore); |
| 2211 | } |
| 2212 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 2213 | InsertBefore); |
| 2214 | } |
| 2215 | |
| 2216 | CmpInst * |
| 2217 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 2218 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 2219 | if (Op == Instruction::ICmp) { |
| 2220 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 2221 | InsertAtEnd); |
| 2222 | } |
| 2223 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 2224 | InsertAtEnd); |
| 2225 | } |
| 2226 | |
| 2227 | void CmpInst::swapOperands() { |
| 2228 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2229 | IC->swapOperands(); |
| 2230 | else |
| 2231 | cast<FCmpInst>(this)->swapOperands(); |
| 2232 | } |
| 2233 | |
| 2234 | bool CmpInst::isCommutative() { |
| 2235 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2236 | return IC->isCommutative(); |
| 2237 | return cast<FCmpInst>(this)->isCommutative(); |
| 2238 | } |
| 2239 | |
| 2240 | bool CmpInst::isEquality() { |
| 2241 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2242 | return IC->isEquality(); |
| 2243 | return cast<FCmpInst>(this)->isEquality(); |
| 2244 | } |
| 2245 | |
| 2246 | |
| 2247 | ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) { |
| 2248 | switch (pred) { |
| 2249 | default: |
| 2250 | assert(!"Unknown icmp predicate!"); |
| 2251 | case ICMP_EQ: return ICMP_NE; |
| 2252 | case ICMP_NE: return ICMP_EQ; |
| 2253 | case ICMP_UGT: return ICMP_ULE; |
| 2254 | case ICMP_ULT: return ICMP_UGE; |
| 2255 | case ICMP_UGE: return ICMP_ULT; |
| 2256 | case ICMP_ULE: return ICMP_UGT; |
| 2257 | case ICMP_SGT: return ICMP_SLE; |
| 2258 | case ICMP_SLT: return ICMP_SGE; |
| 2259 | case ICMP_SGE: return ICMP_SLT; |
| 2260 | case ICMP_SLE: return ICMP_SGT; |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) { |
| 2265 | switch (pred) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2266 | default: assert(! "Unknown icmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2267 | case ICMP_EQ: case ICMP_NE: |
| 2268 | return pred; |
| 2269 | case ICMP_SGT: return ICMP_SLT; |
| 2270 | case ICMP_SLT: return ICMP_SGT; |
| 2271 | case ICMP_SGE: return ICMP_SLE; |
| 2272 | case ICMP_SLE: return ICMP_SGE; |
| 2273 | case ICMP_UGT: return ICMP_ULT; |
| 2274 | case ICMP_ULT: return ICMP_UGT; |
| 2275 | case ICMP_UGE: return ICMP_ULE; |
| 2276 | case ICMP_ULE: return ICMP_UGE; |
| 2277 | } |
| 2278 | } |
| 2279 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2280 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 2281 | switch (pred) { |
| 2282 | default: assert(! "Unknown icmp predicate!"); |
| 2283 | case ICMP_EQ: case ICMP_NE: |
| 2284 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2285 | return pred; |
| 2286 | case ICMP_UGT: return ICMP_SGT; |
| 2287 | case ICMP_ULT: return ICMP_SLT; |
| 2288 | case ICMP_UGE: return ICMP_SGE; |
| 2289 | case ICMP_ULE: return ICMP_SLE; |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | bool ICmpInst::isSignedPredicate(Predicate pred) { |
| 2294 | switch (pred) { |
| 2295 | default: assert(! "Unknown icmp predicate!"); |
| 2296 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2297 | return true; |
| 2298 | case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT: |
| 2299 | case ICMP_UGE: case ICMP_ULE: |
| 2300 | return false; |
| 2301 | } |
| 2302 | } |
| 2303 | |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2304 | /// Initialize a set of values that all satisfy the condition with C. |
| 2305 | /// |
| 2306 | ConstantRange |
| 2307 | ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { |
| 2308 | APInt Lower(C); |
| 2309 | APInt Upper(C); |
| 2310 | uint32_t BitWidth = C.getBitWidth(); |
| 2311 | switch (pred) { |
| 2312 | default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!"); |
| 2313 | case ICmpInst::ICMP_EQ: Upper++; break; |
| 2314 | case ICmpInst::ICMP_NE: Lower++; break; |
| 2315 | case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break; |
| 2316 | case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break; |
| 2317 | case ICmpInst::ICMP_UGT: |
| 2318 | Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
| 2319 | break; |
| 2320 | case ICmpInst::ICMP_SGT: |
| 2321 | Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
| 2322 | break; |
| 2323 | case ICmpInst::ICMP_ULE: |
| 2324 | Lower = APInt::getMinValue(BitWidth); Upper++; |
| 2325 | break; |
| 2326 | case ICmpInst::ICMP_SLE: |
| 2327 | Lower = APInt::getSignedMinValue(BitWidth); Upper++; |
| 2328 | break; |
| 2329 | case ICmpInst::ICMP_UGE: |
| 2330 | Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
| 2331 | break; |
| 2332 | case ICmpInst::ICMP_SGE: |
| 2333 | Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
| 2334 | break; |
| 2335 | } |
| 2336 | return ConstantRange(Lower, Upper); |
| 2337 | } |
| 2338 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2339 | FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) { |
| 2340 | switch (pred) { |
| 2341 | default: |
| 2342 | assert(!"Unknown icmp predicate!"); |
| 2343 | case FCMP_OEQ: return FCMP_UNE; |
| 2344 | case FCMP_ONE: return FCMP_UEQ; |
| 2345 | case FCMP_OGT: return FCMP_ULE; |
| 2346 | case FCMP_OLT: return FCMP_UGE; |
| 2347 | case FCMP_OGE: return FCMP_ULT; |
| 2348 | case FCMP_OLE: return FCMP_UGT; |
| 2349 | case FCMP_UEQ: return FCMP_ONE; |
| 2350 | case FCMP_UNE: return FCMP_OEQ; |
| 2351 | case FCMP_UGT: return FCMP_OLE; |
| 2352 | case FCMP_ULT: return FCMP_OGE; |
| 2353 | case FCMP_UGE: return FCMP_OLT; |
| 2354 | case FCMP_ULE: return FCMP_OGT; |
| 2355 | case FCMP_ORD: return FCMP_UNO; |
| 2356 | case FCMP_UNO: return FCMP_ORD; |
| 2357 | case FCMP_TRUE: return FCMP_FALSE; |
| 2358 | case FCMP_FALSE: return FCMP_TRUE; |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) { |
| 2363 | switch (pred) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2364 | default: assert(!"Unknown fcmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2365 | case FCMP_FALSE: case FCMP_TRUE: |
| 2366 | case FCMP_OEQ: case FCMP_ONE: |
| 2367 | case FCMP_UEQ: case FCMP_UNE: |
| 2368 | case FCMP_ORD: case FCMP_UNO: |
| 2369 | return pred; |
| 2370 | case FCMP_OGT: return FCMP_OLT; |
| 2371 | case FCMP_OLT: return FCMP_OGT; |
| 2372 | case FCMP_OGE: return FCMP_OLE; |
| 2373 | case FCMP_OLE: return FCMP_OGE; |
| 2374 | case FCMP_UGT: return FCMP_ULT; |
| 2375 | case FCMP_ULT: return FCMP_UGT; |
| 2376 | case FCMP_UGE: return FCMP_ULE; |
| 2377 | case FCMP_ULE: return FCMP_UGE; |
| 2378 | } |
| 2379 | } |
| 2380 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2381 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 2382 | switch (predicate) { |
| 2383 | default: return false; |
| 2384 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 2385 | case ICmpInst::ICMP_UGE: return true; |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | bool CmpInst::isSigned(unsigned short predicate){ |
| 2390 | switch (predicate) { |
| 2391 | default: return false; |
| 2392 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 2393 | case ICmpInst::ICMP_SGE: return true; |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 2398 | switch (predicate) { |
| 2399 | default: return false; |
| 2400 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 2401 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 2402 | case FCmpInst::FCMP_ORD: return true; |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 2407 | switch (predicate) { |
| 2408 | default: return false; |
| 2409 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 2410 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 2411 | case FCmpInst::FCMP_UNO: return true; |
| 2412 | } |
| 2413 | } |
| 2414 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2415 | //===----------------------------------------------------------------------===// |
| 2416 | // SwitchInst Implementation |
| 2417 | //===----------------------------------------------------------------------===// |
| 2418 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2419 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2420 | assert(Value && Default); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2421 | ReservedSpace = 2+NumCases*2; |
| 2422 | NumOperands = 2; |
| 2423 | OperandList = new Use[ReservedSpace]; |
| 2424 | |
| 2425 | OperandList[0].init(Value, this); |
| 2426 | OperandList[1].init(Default, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2429 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2430 | /// switch on and a default destination. The number of additional cases can |
| 2431 | /// be specified here to make memory allocation more efficient. This |
| 2432 | /// constructor can also autoinsert before another instruction. |
| 2433 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2434 | Instruction *InsertBefore) |
| 2435 | : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) { |
| 2436 | init(Value, Default, NumCases); |
| 2437 | } |
| 2438 | |
| 2439 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2440 | /// switch on and a default destination. The number of additional cases can |
| 2441 | /// be specified here to make memory allocation more efficient. This |
| 2442 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 2443 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2444 | BasicBlock *InsertAtEnd) |
| 2445 | : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) { |
| 2446 | init(Value, Default, NumCases); |
| 2447 | } |
| 2448 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2449 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2450 | : TerminatorInst(Type::VoidTy, Instruction::Switch, |
| 2451 | new Use[SI.getNumOperands()], SI.getNumOperands()) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2452 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 2453 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
| 2454 | OL[i].init(InOL[i], this); |
| 2455 | OL[i+1].init(InOL[i+1], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2456 | } |
| 2457 | } |
| 2458 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2459 | SwitchInst::~SwitchInst() { |
| 2460 | delete [] OperandList; |
| 2461 | } |
| 2462 | |
| 2463 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2464 | /// addCase - Add an entry to the switch instruction... |
| 2465 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 2466 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2467 | unsigned OpNo = NumOperands; |
| 2468 | if (OpNo+2 > ReservedSpace) |
| 2469 | resizeOperands(0); // Get more space! |
| 2470 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2471 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2472 | NumOperands = OpNo+2; |
| 2473 | OperandList[OpNo].init(OnVal, this); |
| 2474 | OperandList[OpNo+1].init(Dest, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | /// removeCase - This method removes the specified successor from the switch |
| 2478 | /// instruction. Note that this cannot be used to remove the default |
| 2479 | /// destination (successor #0). |
| 2480 | /// |
| 2481 | void SwitchInst::removeCase(unsigned idx) { |
| 2482 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2483 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 2484 | |
| 2485 | unsigned NumOps = getNumOperands(); |
| 2486 | Use *OL = OperandList; |
| 2487 | |
| 2488 | // Move everything after this operand down. |
| 2489 | // |
| 2490 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 2491 | // client might not expect this to happen. The code as it is thrashes the |
| 2492 | // use/def lists, which is kinda lame. |
| 2493 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 2494 | OL[i-2] = OL[i]; |
| 2495 | OL[i-2+1] = OL[i+1]; |
| 2496 | } |
| 2497 | |
| 2498 | // Nuke the last value. |
| 2499 | OL[NumOps-2].set(0); |
| 2500 | OL[NumOps-2+1].set(0); |
| 2501 | NumOperands = NumOps-2; |
| 2502 | } |
| 2503 | |
| 2504 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 2505 | /// list according to the following behavior: |
| 2506 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 2507 | /// of operation. This grows the number of ops by 1.5 times. |
| 2508 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 2509 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 2510 | /// |
| 2511 | void SwitchInst::resizeOperands(unsigned NumOps) { |
| 2512 | if (NumOps == 0) { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2513 | NumOps = getNumOperands()/2*6; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2514 | } else if (NumOps*2 > NumOperands) { |
| 2515 | // No resize needed. |
| 2516 | if (ReservedSpace >= NumOps) return; |
| 2517 | } else if (NumOps == NumOperands) { |
| 2518 | if (ReservedSpace == NumOps) return; |
| 2519 | } else { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2520 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | ReservedSpace = NumOps; |
| 2524 | Use *NewOps = new Use[NumOps]; |
| 2525 | Use *OldOps = OperandList; |
| 2526 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2527 | NewOps[i].init(OldOps[i], this); |
| 2528 | OldOps[i].set(0); |
| 2529 | } |
| 2530 | delete [] OldOps; |
| 2531 | OperandList = NewOps; |
| 2532 | } |
| 2533 | |
| 2534 | |
| 2535 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 2536 | return getSuccessor(idx); |
| 2537 | } |
| 2538 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 2539 | return getNumSuccessors(); |
| 2540 | } |
| 2541 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 2542 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2543 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2544 | |
| 2545 | |
| 2546 | // Define these methods here so vtables don't get emitted into every translation |
| 2547 | // unit that uses these classes. |
| 2548 | |
| 2549 | GetElementPtrInst *GetElementPtrInst::clone() const { |
| 2550 | return new GetElementPtrInst(*this); |
| 2551 | } |
| 2552 | |
| 2553 | BinaryOperator *BinaryOperator::clone() const { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2554 | return create(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Chris Lattner | 0b490b0 | 2007-08-24 20:48:18 +0000 | [diff] [blame] | 2557 | FCmpInst* FCmpInst::clone() const { |
| 2558 | return new FCmpInst(getPredicate(), Ops[0], Ops[1]); |
| 2559 | } |
| 2560 | ICmpInst* ICmpInst::clone() const { |
| 2561 | return new ICmpInst(getPredicate(), Ops[0], Ops[1]); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2564 | MallocInst *MallocInst::clone() const { return new MallocInst(*this); } |
| 2565 | AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } |
| 2566 | FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } |
| 2567 | LoadInst *LoadInst::clone() const { return new LoadInst(*this); } |
| 2568 | StoreInst *StoreInst::clone() const { return new StoreInst(*this); } |
| 2569 | CastInst *TruncInst::clone() const { return new TruncInst(*this); } |
| 2570 | CastInst *ZExtInst::clone() const { return new ZExtInst(*this); } |
| 2571 | CastInst *SExtInst::clone() const { return new SExtInst(*this); } |
| 2572 | CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); } |
| 2573 | CastInst *FPExtInst::clone() const { return new FPExtInst(*this); } |
| 2574 | CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); } |
| 2575 | CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); } |
| 2576 | CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); } |
| 2577 | CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); } |
| 2578 | CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); } |
| 2579 | CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); } |
| 2580 | CastInst *BitCastInst::clone() const { return new BitCastInst(*this); } |
| 2581 | CallInst *CallInst::clone() const { return new CallInst(*this); } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2582 | SelectInst *SelectInst::clone() const { return new SelectInst(*this); } |
| 2583 | VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } |
| 2584 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2585 | ExtractElementInst *ExtractElementInst::clone() const { |
| 2586 | return new ExtractElementInst(*this); |
| 2587 | } |
| 2588 | InsertElementInst *InsertElementInst::clone() const { |
| 2589 | return new InsertElementInst(*this); |
| 2590 | } |
| 2591 | ShuffleVectorInst *ShuffleVectorInst::clone() const { |
| 2592 | return new ShuffleVectorInst(*this); |
| 2593 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2594 | PHINode *PHINode::clone() const { return new PHINode(*this); } |
| 2595 | ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); } |
| 2596 | BranchInst *BranchInst::clone() const { return new BranchInst(*this); } |
| 2597 | SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } |
| 2598 | InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } |
| 2599 | UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 2600 | UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} |