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