OLD | NEW |
1 //===- FlattenGlobals.cpp - Flatten global variable initializers-----------===// | 1 //===- FlattenGlobals.cpp - Flatten global variable initializers-----------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This pass converts initializers for global variables into a | 10 // This pass converts initializers for global variables into a |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 static void ExpandConstant(const DataLayout *DL, Constant *Val, | 343 static void ExpandConstant(const DataLayout *DL, Constant *Val, |
344 Constant **ResultGlobal, uint64_t *ResultOffset) { | 344 Constant **ResultGlobal, uint64_t *ResultOffset) { |
345 if (isa<GlobalValue>(Val) || isa<BlockAddress>(Val)) { | 345 if (isa<GlobalValue>(Val) || isa<BlockAddress>(Val)) { |
346 *ResultGlobal = Val; | 346 *ResultGlobal = Val; |
347 *ResultOffset = 0; | 347 *ResultOffset = 0; |
348 } else if (isa<ConstantPointerNull>(Val)) { | 348 } else if (isa<ConstantPointerNull>(Val)) { |
349 *ResultGlobal = NULL; | 349 *ResultGlobal = NULL; |
350 *ResultOffset = 0; | 350 *ResultOffset = 0; |
351 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { | 351 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
352 *ResultGlobal = NULL; | 352 *ResultGlobal = NULL; |
353 *ResultOffset = CI->getZExtValue(); | 353 *ResultOffset = CI->getSExtValue(); |
354 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Val)) { | 354 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Val)) { |
355 ExpandConstant(DL, CE->getOperand(0), ResultGlobal, ResultOffset); | 355 ExpandConstant(DL, CE->getOperand(0), ResultGlobal, ResultOffset); |
356 if (CE->getOpcode() == Instruction::GetElementPtr) { | 356 if (CE->getOpcode() == Instruction::GetElementPtr) { |
357 SmallVector<Value *, 8> Indexes(CE->op_begin() + 1, CE->op_end()); | 357 SmallVector<Value *, 8> Indexes(CE->op_begin() + 1, CE->op_end()); |
358 *ResultOffset += DL->getIndexedOffset(CE->getOperand(0)->getType(), | 358 *ResultOffset += DL->getIndexedOffset(CE->getOperand(0)->getType(), |
359 Indexes); | 359 Indexes); |
| 360 } else if (CE->getOpcode() == Instruction::Add && |
| 361 isa<ConstantInt>(CE->getOperand(1))) { |
| 362 *ResultOffset += cast<ConstantInt>(CE->getOperand(1))->getSExtValue(); |
360 } else if (CE->getOpcode() == Instruction::BitCast || | 363 } else if (CE->getOpcode() == Instruction::BitCast || |
361 CE->getOpcode() == Instruction::IntToPtr) { | 364 CE->getOpcode() == Instruction::IntToPtr) { |
362 // Nothing more to do. | 365 // Nothing more to do. |
363 } else if (CE->getOpcode() == Instruction::PtrToInt) { | 366 } else if (CE->getOpcode() == Instruction::PtrToInt) { |
364 if (Val->getType()->getIntegerBitWidth() < DL->getPointerSizeInBits()) { | 367 if (Val->getType()->getIntegerBitWidth() < DL->getPointerSizeInBits()) { |
365 errs() << "Not handled: " << *CE << "\n"; | 368 errs() << "Not handled: " << *CE << "\n"; |
366 report_fatal_error("FlattenGlobals: a ptrtoint that truncates " | 369 report_fatal_error("FlattenGlobals: a ptrtoint that truncates " |
367 "a pointer is not allowed"); | 370 "a pointer is not allowed"); |
368 } | 371 } |
369 } else { | 372 } else { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 State.flattenGlobalsWithInitializers(); | 540 State.flattenGlobalsWithInitializers(); |
538 State.removeDeadInitializerConstants(); | 541 State.removeDeadInitializerConstants(); |
539 State.replaceGlobalsWithFlattenedGlobals(); | 542 State.replaceGlobalsWithFlattenedGlobals(); |
540 State.installFlattenedGlobalInitializers(); | 543 State.installFlattenedGlobalInitializers(); |
541 return State.Modified; | 544 return State.Modified; |
542 } | 545 } |
543 | 546 |
544 ModulePass *llvm::createFlattenGlobalsPass() { | 547 ModulePass *llvm::createFlattenGlobalsPass() { |
545 return new FlattenGlobals(); | 548 return new FlattenGlobals(); |
546 } | 549 } |
OLD | NEW |